forked from Gernott/mask
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
In order to prepare a future sorting of elements, a usort function was used together with a new sorting key to order the elements. However, this led to arbitrary order of elements in PHP version less than 8. In php 8 a stable sorting was introduced. To prevent a changing order when updating, a stable usort has been added, which takes effect on lower php versions.
- Loading branch information
Showing
3 changed files
with
241 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
211 changes: 211 additions & 0 deletions
211
Tests/Unit/Definition/ElementDefinitionCollectionTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,211 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of the TYPO3 CMS project. | ||
* | ||
* It is free software; you can redistribute it and/or modify it under | ||
* the terms of the GNU General Public License, either version 2 | ||
* of the License, or any later version. | ||
* | ||
* For the full copyright and license information, please read the | ||
* LICENSE.txt file that was distributed with this source code. | ||
* | ||
* The TYPO3 project - inspiring people to share! | ||
*/ | ||
|
||
namespace MASK\Mask\Tests\Unit\Definition; | ||
|
||
use MASK\Mask\Definition\ElementDefinitionCollection; | ||
use TYPO3\TestingFramework\Core\Unit\UnitTestCase; | ||
|
||
class ElementDefinitionCollectionTest extends UnitTestCase | ||
{ | ||
public function elementsAreSortedBySortingDataProvider(): iterable | ||
{ | ||
yield 'Sorting of elements is preserved without sorting set' => [ | ||
'json' => [ | ||
'element1' => [ | ||
'key' => 'element1', | ||
'label' => 'Element1' | ||
], | ||
'element2' => [ | ||
'key' => 'element2', | ||
'label' => 'Element2' | ||
], | ||
'element3' => [ | ||
'key' => 'element3', | ||
'label' => 'Element3' | ||
], | ||
], | ||
'expected' => [ | ||
[ | ||
'key' => 'element1', | ||
'label' => 'Element1', | ||
'description' => '', | ||
'shortLabel' => '', | ||
'color' => '', | ||
'icon' => '', | ||
'columns' => [], | ||
'labels' => [], | ||
'descriptions' => [], | ||
'sorting' => 0, | ||
], | ||
[ | ||
'key' => 'element2', | ||
'label' => 'Element2', | ||
'description' => '', | ||
'shortLabel' => '', | ||
'color' => '', | ||
'icon' => '', | ||
'columns' => [], | ||
'labels' => [], | ||
'descriptions' => [], | ||
'sorting' => 0, | ||
], | ||
[ | ||
'key' => 'element3', | ||
'label' => 'Element3', | ||
'description' => '', | ||
'shortLabel' => '', | ||
'color' => '', | ||
'icon' => '', | ||
'columns' => [], | ||
'labels' => [], | ||
'descriptions' => [], | ||
'sorting' => 0, | ||
], | ||
] | ||
]; | ||
|
||
yield 'Sorting of elements is preserved with sorting set' => [ | ||
'json' => [ | ||
'element1' => [ | ||
'key' => 'element1', | ||
'label' => 'Element1', | ||
'sorting' => '0', | ||
], | ||
'element2' => [ | ||
'key' => 'element2', | ||
'label' => 'Element2', | ||
'sorting' => '0', | ||
], | ||
'element3' => [ | ||
'key' => 'element3', | ||
'label' => 'Element3', | ||
'sorting' => '0', | ||
], | ||
], | ||
'expected' => [ | ||
[ | ||
'key' => 'element1', | ||
'label' => 'Element1', | ||
'description' => '', | ||
'shortLabel' => '', | ||
'color' => '', | ||
'icon' => '', | ||
'columns' => [], | ||
'labels' => [], | ||
'descriptions' => [], | ||
'sorting' => 0, | ||
], | ||
[ | ||
'key' => 'element2', | ||
'label' => 'Element2', | ||
'description' => '', | ||
'shortLabel' => '', | ||
'color' => '', | ||
'icon' => '', | ||
'columns' => [], | ||
'labels' => [], | ||
'descriptions' => [], | ||
'sorting' => 0, | ||
], | ||
[ | ||
'key' => 'element3', | ||
'label' => 'Element3', | ||
'description' => '', | ||
'shortLabel' => '', | ||
'color' => '', | ||
'icon' => '', | ||
'columns' => [], | ||
'labels' => [], | ||
'descriptions' => [], | ||
'sorting' => 0, | ||
], | ||
] | ||
]; | ||
|
||
yield 'Sorting of elements is preserved with real sorting set' => [ | ||
'json' => [ | ||
'element1' => [ | ||
'key' => 'element1', | ||
'label' => 'Element1', | ||
'sorting' => '2', | ||
], | ||
'element2' => [ | ||
'key' => 'element2', | ||
'label' => 'Element2', | ||
'sorting' => '1', | ||
], | ||
'element3' => [ | ||
'key' => 'element3', | ||
'label' => 'Element3', | ||
'sorting' => '0', | ||
], | ||
], | ||
'expected' => [ | ||
[ | ||
'key' => 'element3', | ||
'label' => 'Element3', | ||
'description' => '', | ||
'shortLabel' => '', | ||
'color' => '', | ||
'icon' => '', | ||
'columns' => [], | ||
'labels' => [], | ||
'descriptions' => [], | ||
'sorting' => 0, | ||
], | ||
[ | ||
'key' => 'element2', | ||
'label' => 'Element2', | ||
'description' => '', | ||
'shortLabel' => '', | ||
'color' => '', | ||
'icon' => '', | ||
'columns' => [], | ||
'labels' => [], | ||
'descriptions' => [], | ||
'sorting' => 1, | ||
], | ||
[ | ||
'key' => 'element1', | ||
'label' => 'Element1', | ||
'description' => '', | ||
'shortLabel' => '', | ||
'color' => '', | ||
'icon' => '', | ||
'columns' => [], | ||
'labels' => [], | ||
'descriptions' => [], | ||
'sorting' => 2, | ||
], | ||
] | ||
]; | ||
} | ||
|
||
/** | ||
* @dataProvider elementsAreSortedBySortingDataProvider | ||
* @test | ||
*/ | ||
public function elementsAreSortedBySorting(array $json, array $expected): void | ||
{ | ||
$elements = []; | ||
foreach (ElementDefinitionCollection::createFromArray($json, 'tt_content') as $element) { | ||
$elements[] = $element->toArray(); | ||
} | ||
self::assertEquals($expected, $elements); | ||
} | ||
} |