jenkins-bot has submitted this change and it was merged. ( https://gerrit.wikimedia.org/r/328183 )
Change subject: Rewrite DataTypeSelector ...................................................................... Rewrite DataTypeSelector It's used entirely different now that both users are changed to be based on OOUI. I also added much more test cases that actually test the sorting feature. Change-Id: Ibda7d52be41695cf57a61649f26988b5be2f451e --- M repo/includes/DataTypeSelector.php M repo/includes/Specials/SpecialListProperties.php M repo/includes/Specials/SpecialNewProperty.php M repo/tests/phpunit/includes/DataTypeSelectorTest.php 4 files changed, 66 insertions(+), 34 deletions(-) Approvals: Aleksey Bekh-Ivanov (WMDE): Looks good to me, approved jenkins-bot: Verified diff --git a/repo/includes/DataTypeSelector.php b/repo/includes/DataTypeSelector.php index bdee4e1..920f646 100644 --- a/repo/includes/DataTypeSelector.php +++ b/repo/includes/DataTypeSelector.php @@ -6,12 +6,13 @@ use MWException; /** - * DataType selector UI element. + * Data provider for the property type (a.k.a. data type) selector UI element. * * @since 0.4 * * @license GPL-2.0+ * @author Jeroen De Dauw < [email protected] > + * @author Thiemo Mättig */ class DataTypeSelector { @@ -51,18 +52,28 @@ /** * Builds and returns the array for the options of the DataType selector. * - * @return array + * @since 0.5 + * + * @return string[] */ public function getOptionsArray() { - $dataTypes = array(); + $byLabel = []; + $byId = []; foreach ( $this->dataTypes as $dataType ) { - $dataTypes[$dataType->getId()] = $dataType->getLabel( $this->languageCode ); + $label = $dataType->getLabel( $this->languageCode ); + $id = $dataType->getId(); + + $byLabel[$label] = $id; + $byId[$id] = $id; } - natcasesort( $dataTypes ); + if ( count( $byLabel ) < count( $this->dataTypes ) ) { + $byLabel = $byId; + } - return $dataTypes; + uksort( $byLabel, 'strnatcasecmp' ); + return $byLabel; } } diff --git a/repo/includes/Specials/SpecialListProperties.php b/repo/includes/Specials/SpecialListProperties.php index 4b2ba70..824734e 100644 --- a/repo/includes/Specials/SpecialListProperties.php +++ b/repo/includes/Specials/SpecialListProperties.php @@ -151,7 +151,7 @@ $options = array( $this->msg( 'wikibase-listproperties-all' )->text() => '' ); - $options = array_merge( $options, array_flip( $dataTypeSelect->getOptionsArray() ) ); + $options = array_merge( $options, $dataTypeSelect->getOptionsArray() ); $formDescriptor = array( 'datatype' => array( diff --git a/repo/includes/Specials/SpecialNewProperty.php b/repo/includes/Specials/SpecialNewProperty.php index 8c8e1cf..76d113b 100644 --- a/repo/includes/Specials/SpecialNewProperty.php +++ b/repo/includes/Specials/SpecialNewProperty.php @@ -124,7 +124,7 @@ 'name' => self::FIELD_DATATYPE, 'type' => 'select', 'default' => isset( $this->parts[2] ) ? $this->parts[2] : 'string', - 'options' => array_flip( $selector->getOptionsArray() ), + 'options' => $selector->getOptionsArray(), 'id' => 'wb-newproperty-datatype', 'validation-callback' => function ( $dataType, $formData, $form ) { if ( !$this->dataTypeExists( $dataType ) ) { diff --git a/repo/tests/phpunit/includes/DataTypeSelectorTest.php b/repo/tests/phpunit/includes/DataTypeSelectorTest.php index c9dcd73..fc05140 100644 --- a/repo/tests/phpunit/includes/DataTypeSelectorTest.php +++ b/repo/tests/phpunit/includes/DataTypeSelectorTest.php @@ -1,6 +1,6 @@ <?php -namespace Wikibase\Test; +namespace Wikibase\Repo\Tests; use DataTypes\DataType; use MWException; @@ -20,10 +20,11 @@ /** * @param string $propertyType + * @param string $label * * @return DataType */ - private function newDataType( $propertyType ) { + private function newDataType( $propertyType, $label ) { $dataType = $this->getMockBuilder( DataType::class ) ->disableOriginalConstructor() ->getMock(); @@ -34,21 +35,10 @@ $dataType->expects( $this->any() ) ->method( 'getLabel' ) - ->will( $this->returnValue( '(datatypes-type-' . $propertyType . ')' ) ); + ->with( 'en' ) + ->will( $this->returnValue( $label ) ); return $dataType; - } - - /** - * @param DataType[]|null $dataTypes - * - * @return DataTypeSelector - */ - private function newInstance( array $dataTypes = null ) { - return new DataTypeSelector( - $dataTypes !== null ? $dataTypes : array( $this->newDataType( '<PT>' ) ), - 'qqx' - ); } /** @@ -60,19 +50,50 @@ } public function invalidConstructorArgumentsProvider() { - return array( - array( array(), null ), - array( array(), false ), - array( array( null ), '' ), - array( array( false ), '' ), - array( array( '' ), '' ), - ); + return [ + [ [], null ], + [ [], false ], + [ [ null ], '' ], + [ [ false ], '' ], + [ [ '' ], '' ], + ]; } - public function testGetOptionsArray() { - $selector = $this->newInstance(); - $options = $selector->getOptionsArray(); - $this->assertSame( array( '<PT>' => '(datatypes-type-<PT>)' ), $options ); + public function testGetOptionsArrayWithOneElement() { + $selector = new DataTypeSelector( [ + $this->newDataType( '<PROPERTY-TYPE>', '<LABEL>' ), + ], 'en' ); + + $expected = [ + '<LABEL>' => '<PROPERTY-TYPE>', + ]; + $this->assertSame( $expected, $selector->getOptionsArray() ); + } + + public function testGetOptionsArrayWithDuplicateLabels() { + $selector = new DataTypeSelector( [ + $this->newDataType( '<PROPERTY-TYPE-B>', '<LABEL>' ), + $this->newDataType( '<PROPERTY-TYPE-A>', '<LABEL>' ), + ], 'en' ); + + $expected = [ + '<PROPERTY-TYPE-A>' => '<PROPERTY-TYPE-A>', + '<PROPERTY-TYPE-B>' => '<PROPERTY-TYPE-B>', + ]; + $this->assertSame( $expected, $selector->getOptionsArray() ); + } + + public function testGetOptionsArraySortsLabelsInNaturalOrder() { + $selector = new DataTypeSelector( [ + $this->newDataType( '<PROPERTY-TYPE-A>', '<LABEL-10>' ), + $this->newDataType( '<PROPERTY-TYPE-B>', '<label-2>' ), + ], 'en' ); + + $expected = [ + '<label-2>' => '<PROPERTY-TYPE-B>', + '<LABEL-10>' => '<PROPERTY-TYPE-A>', + ]; + $this->assertSame( $expected, $selector->getOptionsArray() ); } } -- To view, visit https://gerrit.wikimedia.org/r/328183 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: merged Gerrit-Change-Id: Ibda7d52be41695cf57a61649f26988b5be2f451e Gerrit-PatchSet: 3 Gerrit-Project: mediawiki/extensions/Wikibase Gerrit-Branch: master Gerrit-Owner: Thiemo Mättig (WMDE) <[email protected]> Gerrit-Reviewer: Addshore <[email protected]> Gerrit-Reviewer: Aleksey Bekh-Ivanov (WMDE) <[email protected]> Gerrit-Reviewer: Aude <[email protected]> Gerrit-Reviewer: Thiemo Mättig (WMDE) <[email protected]> Gerrit-Reviewer: WMDE-leszek <[email protected]> Gerrit-Reviewer: jenkins-bot <> _______________________________________________ MediaWiki-commits mailing list [email protected] https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits
