1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace DNADesign\Elemental\Tests\Services; |
4
|
|
|
|
5
|
|
|
use DNADesign\Elemental\Services\ReorderElements; |
6
|
|
|
use DNADesign\Elemental\Tests\Src\TestElement; |
7
|
|
|
use SilverStripe\Dev\SapphireTest; |
8
|
|
|
|
9
|
|
|
class ReorderElementsTest extends SapphireTest |
10
|
|
|
{ |
11
|
|
|
protected static $fixture_file = 'ReorderElementsTest.yml'; |
12
|
|
|
|
13
|
|
|
protected static $extra_dataobjects = [ |
14
|
|
|
TestElement::class, |
15
|
|
|
]; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Reorders blocks by compounding each result into the next test (rather than isolated sorting tests) |
19
|
|
|
*/ |
20
|
|
|
public function testReorder() |
21
|
|
|
{ |
22
|
|
|
$element = $this->objFromFixture(TestElement::class, 'element1'); |
23
|
|
|
$reorderService = new ReorderElements($element); |
24
|
|
|
|
25
|
|
|
$reorderService->reorder(4); |
26
|
|
|
$this->assertIdsInOrder([2, 3, 4, 1], 'The Element should be last in the list'); |
27
|
|
|
|
28
|
|
|
$reorderService->reorder(0); |
29
|
|
|
$this->assertIdsInOrder([1, 2, 3, 4], 'The Element should be first in the list'); |
30
|
|
|
|
31
|
|
|
$reorderService->reorder(2); |
32
|
|
|
$this->assertIdsInOrder([2, 1, 3, 4], 'The Element should be second in the list'); |
33
|
|
|
|
34
|
|
|
$reorderService->reorder(); |
35
|
|
|
$this->assertIdsInOrder([1, 2, 3, 4], 'The Element should be first in the list'); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
protected function assertIdsInOrder($ids, $message = null) |
39
|
|
|
{ |
40
|
|
|
$actualIDs = TestElement::get()->sort('Sort')->column('ID'); |
41
|
|
|
|
42
|
|
|
// Ideally this should be assertSame, but sometimes the database |
43
|
|
|
// returns IDs as strings instead of integers (e.g. "1" instead of 1). |
44
|
|
|
$this->assertEquals($ids, $actualIDs, $message); |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
|