Thiemo Mättig (WMDE) has uploaded a new change for review. ( 
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, 69 insertions(+), 33 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Wikibase 
refs/changes/83/328183/1

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 cd5f675..7622232 100644
--- a/repo/includes/Specials/SpecialNewProperty.php
+++ b/repo/includes/Specials/SpecialNewProperty.php
@@ -112,7 +112,7 @@
                        'name' => 'datatype',
                        'type' => 'select',
                        'default' => $this->dataType,
-                       'options' => array_flip( $selector->getOptionsArray() ),
+                       'options' => $selector->getOptionsArray(),
                        'id' => 'wb-newproperty-datatype',
                        'label-message' => 'wikibase-newproperty-datatype'
                ];
diff --git a/repo/tests/phpunit/includes/DataTypeSelectorTest.php 
b/repo/tests/phpunit/includes/DataTypeSelectorTest.php
index c9dcd73..4f873f4 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,9 @@
 
                $dataType->expects( $this->any() )
                        ->method( 'getLabel' )
-                       ->will( $this->returnValue( '(datatypes-type-' . 
$propertyType . ')' ) );
+                       ->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 +49,55 @@
        }
 
        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();
+       /**
+        * @dataProvider getOptionsArrayProvider
+        */
+       public function testGetOptionsArray( array $dataTypes, array $expected 
) {
+               $selector = new DataTypeSelector( $dataTypes, 'qqx' );
                $options = $selector->getOptionsArray();
-               $this->assertSame( array( '<PT>' => '(datatypes-type-<PT>)' ), 
$options );
+               $this->assertSame( $expected, $options );
+       }
+
+       public function getOptionsArrayProvider() {
+               return [
+                       'basic' => [
+                               [
+                                       $this->newDataType( '<PT>', '<LABEL>' ),
+                               ],
+                               [
+                                       '<LABEL>' => '<PT>',
+                               ]
+                       ],
+                       'natcasesort' => [
+                               [
+                                       $this->newDataType( '<PTA>', 
'<LABEL-10>' ),
+                                       $this->newDataType( '<PTB>', 
'<label-2>' ),
+                               ],
+                               [
+                                       '<label-2>' => '<PTB>',
+                                       '<LABEL-10>' => '<PTA>',
+                               ]
+                       ],
+                       'duplicate labels' => [
+                               [
+                                       $this->newDataType( '<PTB>', '<LABEL>' 
),
+                                       $this->newDataType( '<PTA>', '<LABEL>' 
),
+                               ],
+                               [
+                                       '<PTA>' => '<PTA>',
+                                       '<PTB>' => '<PTB>',
+                               ]
+                       ],
+               ];
        }
 
 }

-- 
To view, visit https://gerrit.wikimedia.org/r/328183
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ibda7d52be41695cf57a61649f26988b5be2f451e
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: master
Gerrit-Owner: Thiemo Mättig (WMDE) <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to