Skip to content

Commit

Permalink
NEW Adding tests for new GraphQL mutation creator
Browse files Browse the repository at this point in the history
  • Loading branch information
ScopeyNZ committed Sep 17, 2018
1 parent 4e1267c commit ea66a55
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 5 deletions.
10 changes: 5 additions & 5 deletions src/GraphQL/SortBlockMutationCreator.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,20 +77,20 @@ public function resolve($object, array $args, $context, ResolveInfo $info)

if ($sortAfterPosition < $blockPosition) {
$operator = '+';
$filter = "Sort > $sortAfterPosition && Sort < $blockPosition";
$filter = "\"Sort\" > $sortAfterPosition AND \"Sort\" < $blockPosition";
$newBlockPosition = $sortAfterPosition + 1;
} else {
$operator = '-';
$filter = "Sort <= $sortAfterPosition && Sort > $blockPosition";
$filter = "\"Sort\" <= $sortAfterPosition AND \"Sort\" > $blockPosition";
$newBlockPosition = $sortAfterPosition;
}

$table = DataObject::getSchema()->tableName(BaseElement::class);

$query = SQLUpdate::create()
->setTable($table)
->assignSQL('"' . $table . '"."Sort"', "\"$table\".\"Sort\" $operator 1")
->addWhere([$filter, '"ParentId"' => $parentId]);
->setTable("\"$table\"")
->assignSQL('"Sort"', "\"Sort\" $operator 1")
->addWhere([$filter, '"ParentID"' => $parentId]);

$query->execute();

Expand Down
60 changes: 60 additions & 0 deletions tests/GraphQL/SortBlockMutationCreatorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php
namespace DNADesign\Elemental\Tests\GraphQL;

use DNADesign\Elemental\GraphQL\SortBlockMutationCreator;
use DNADesign\Elemental\Tests\Src\TestElement;
use GraphQL\Type\Definition\ResolveInfo;
use SilverStripe\Dev\SapphireTest;
use SilverStripe\Security\Security;

class SortBlockMutationCreatorTest extends SapphireTest
{
protected static $fixture_file = 'SortBlockMutationCreatorTest.yml';

protected static $extra_dataobjects = [
TestElement::class
];

protected function setUp()
{
parent::setUp();

$this->logInWithPermission('ADMIN');
}

public function testSortBlock()
{
$this->runMutation(1, 3);
$this->assertIdsInOrder([2,3,1,4]);

$this->runMutation(4, 2);
$this->assertIdsInOrder([2,4,3,1]);

$this->runMutation(1, 0);
$this->assertIdsInOrder([1,2,4,3]);

$this->runMutation(3, 2);
$this->assertIdsInOrder([1,2,3,4]);
}

protected function assertIdsInOrder($ids)
{
$actualIDs = TestElement::get()->sort('Sort')->map()->keys();

$this->assertSame($ids, $actualIDs);
}

protected function runMutation($id, $afterBlockId)
{
$member = Security::getCurrentUser();

$mutation = new SortBlockMutationCreator();
$context = ['currentUser' => $member];
$resolveInfo = new ResolveInfo([]);

$mutation->resolve(null, [
'ID' => $id,
'AfterBlockID' => $afterBlockId,
], $context, $resolveInfo);
}
}
13 changes: 13 additions & 0 deletions tests/GraphQL/SortBlockMutationCreatorTest.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
DNADesign\Elemental\Tests\Src\TestElement:
element1:
ID: 1
Sort: 1
element2:
ID: 2
Sort: 2
element3:
ID: 3
Sort: 3
element4:
ID: 4
Sort: 4

0 comments on commit ea66a55

Please sign in to comment.