Skip to content

Commit

Permalink
Add unit tests for new ManyManyThrough support
Browse files Browse the repository at this point in the history
The previous commit (9fa9ef8) added support for the new SilverStripe 4
feature of Many Many relationships through an intermediary object. After
much head scratching and community testing, the solution was proven to
work, however had no automated tests to confirm as such. This commit
rectifies that by testing both versioned and unversioned DataObjects in
a many_many through style relationship. Some minor tidy and comments
were also added as per feedback on the functionality code changes.
  • Loading branch information
Dylan Wagstaff committed Jul 1, 2018
1 parent 95f0acb commit bba8547
Show file tree
Hide file tree
Showing 8 changed files with 346 additions and 32 deletions.
23 changes: 13 additions & 10 deletions src/GridFieldOrderableRows.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,8 @@ public function getExtraSortFields()
* Checks to see if the relationship list is for a type of many_many
*
* @param SS_List $list
*
* @return bool
*/
protected function isManyMany(SS_List $list)
{
Expand Down Expand Up @@ -497,7 +499,7 @@ public function handleSave(GridField $grid, DataObjectInterface $record)
*/
protected function executeReorder(GridField $grid, $sortedIDs)
{
if (!is_array($sortedIDs)) {
if (!is_array($sortedIDs) || empty($sortedIDs)) {
return false;
}
$sortField = $this->getSortField();
Expand Down Expand Up @@ -544,6 +546,7 @@ protected function executeReorder(GridField $grid, $sortedIDs)
$toRelationName = $manipulator->getLocalKey();
$sortlist = DataList::create($joinClass)->filter([
$toRelationName => $items->column('ID'),
// first() is safe as there are earlier checks to ensure our list to sort is valid
$fromRelationName => $items->first()->getJoin()->$fromRelationName,
]);
$current = $sortlist->map($toRelationName, $sortField)->toArray();
Expand Down Expand Up @@ -587,19 +590,19 @@ protected function reorderItems($list, array $values, array $sortedIDs)
// - Related item is versioned...
// - Through object is versioned: handle as versioned
// - Through object is NOT versioned: THROW an error.
$isManyMany = $this->isManyMany($list);
if ($isManyMany && $list instanceof ManyManyThroughList) {
if ($list instanceof ManyManyThroughList) {
$listClassVersioned = $class::create()->hasExtension(Versioned::class);
// We'll be updating the join class, not the relation class.
$class = $this->getManyManyInspector($list)->getJoinClass();
$isVersioned = $class::create()->hasExtension(Versioned::class);

// If one side of the relationship is versioned and the other is not, throw an error.
if ($listClassVersioned xor $isVersioned) {
throw new Exception(
'ManyManyThrough cannot mismatch Versioning between join class and related class'
);
}
} elseif (!$isManyMany) {
} elseif (!$this->isManyMany($list)) {
$isVersioned = $class::create()->hasExtension(Versioned::class);
}

Expand Down Expand Up @@ -716,12 +719,12 @@ protected function getSortTableClauseForIds(DataList $list, $ids)
}

if ($this->isManyMany($list)) {
$intropector = $this->getManyManyInspector($list);
$introspector = $this->getManyManyInspector($list);
$extra = $list instanceof ManyManyList ?
$intropector->getExtraFields() :
DataObjectSchema::create()->fieldSpecs($intropector->getJoinClass(), DataObjectSchema::DB_ONLY);
$key = $intropector->getLocalKey();
$foreignKey = $intropector->getForeignKey();
$introspector->getExtraFields() :
DataObjectSchema::create()->fieldSpecs($introspector->getJoinClass(), DataObjectSchema::DB_ONLY);
$key = $introspector->getLocalKey();
$foreignKey = $introspector->getForeignKey();
$foreignID = (int) $list->getForeignID();

if ($extra && array_key_exists($this->getSortField(), $extra)) {
Expand All @@ -743,7 +746,7 @@ protected function getSortTableClauseForIds(DataList $list, $ids)
* these functions are moved to ManyManyThroughQueryManipulator, but otherwise retain
* the same signature.
*
* @param ManyManyList|ManyManyThroughList
* @param ManyManyList|ManyManyThroughList $list
*
* @return ManyManyList|ManyManyThroughQueryManipulator
*/
Expand Down
121 changes: 108 additions & 13 deletions tests/GridFieldOrderableRowsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,44 +12,67 @@
use Symbiote\GridFieldExtensions\Tests\Stub\StubParent;
use Symbiote\GridFieldExtensions\Tests\Stub\StubSubclass;
use Symbiote\GridFieldExtensions\Tests\Stub\StubUnorderable;
use Symbiote\GridFieldExtensions\Tests\Stub\ThroughDefiner;
use Symbiote\GridFieldExtensions\Tests\Stub\ThroughIntermediary;
use Symbiote\GridFieldExtensions\Tests\Stub\ThroughBelongs;

/**
* Tests for the {@link GridFieldOrderableRows} component.
*/
class GridFieldOrderableRowsTest extends SapphireTest
{

protected $usesDatabase = true;

protected static $fixture_file = 'GridFieldOrderableRowsTest.yml';
/**
* @var string
*/
protected static $fixture_file = [
'GridFieldOrderableRowsTest.yml',
'OrderableRowsThroughTest.yml'
];

protected static $extra_dataobjects = [
StubParent::class,
StubOrdered::class,
StubSubclass::class,
StubUnorderable::class,
StubOrderableChild::class,
StubOrderedVersioned::class,
StubSubclassOrderedVersioned::class,
ThroughDefiner::class,
ThroughIntermediary::class,
ThroughBelongs::class,
];

public function testReorderItems()
public function reorderItemsProvider()
{
$orderable = new GridFieldOrderableRows('ManyManySort');
return [
[StubParent::class . '.parent', 'MyManyMany', 'ManyManySort'],
[ThroughDefiner::class . '.DefinerOne', 'Belongings', 'Sort'],
];
}

/**
* @dataProvider reorderItemsProvider
*/
public function testReorderItems($fixtureID, $relationName, $sortName)
{
$orderable = new GridFieldOrderableRows($sortName);
$reflection = new ReflectionMethod($orderable, 'executeReorder');
$reflection->setAccessible(true);

$parent = $this->objFromFixture(StubParent::class, 'parent');

$config = new GridFieldConfig_RelationEditor();
$config->addComponent($orderable);

list($parentClass, $parentInstanceID) = explode('.', $fixtureID);
$parent = $this->objFromFixture($parentClass, $parentInstanceID);

$grid = new GridField(
'MyManyMany',
'My Many Many',
$parent->MyManyMany()->sort('ManyManySort'),
$relationName,
'Testing Many Many',
$parent->$relationName()->sort($sortName),
$config
);

$originalOrder = $parent->MyManyMany()->sort('ManyManySort')->column('ID');
$originalOrder = $parent->$relationName()->sort($sortName)->column('ID');
$desiredOrder = [];

// Make order non-contiguous, and 1-based
Expand All @@ -61,7 +84,7 @@ public function testReorderItems()

$reflection->invoke($orderable, $grid, $desiredOrder);

$newOrder = $parent->MyManyMany()->sort('ManyManySort')->map('ManyManySort', 'ID')->toArray();
$newOrder = $parent->$relationName()->sort($sortName)->map($sortName, 'ID')->toArray();

$this->assertEquals($desiredOrder, $newOrder);
}
Expand Down Expand Up @@ -125,5 +148,77 @@ public function testGetSortTable()
'StubParent_MyManyMany',
$orderable->setSortField('ManyManySort')->getSortTable($parent->MyManyMany())
);

$this->assertEquals(
'StubOrderedVersioned',
$orderable->setSortField('Sort')->getSortTable($parent->MyHasManySubclassOrderedVersioned())
);
}

public function testReorderItemsSubclassVersioned()
{
$orderable = new GridFieldOrderableRows('Sort');
$reflection = new ReflectionMethod($orderable, 'executeReorder');
$reflection->setAccessible(true);

$parent = $this->objFromFixture(StubParent::class, 'parent-subclass-ordered-versioned');

// make sure all items are published
foreach ($parent->MyHasManySubclassOrderedVersioned() as $item) {
$item->publishRecursive();
}

// there should be no difference between stages at this point
$differenceFound = false;
foreach ($parent->MyHasManySubclassOrderedVersioned() as $item) {
/** @var StubSubclassOrderedVersioned|Versioned $item */
if ($item->stagesDiffer()) {
$this->fail('Unexpected difference found on stages');
}
}

// reorder items
$config = new GridFieldConfig_RelationEditor();
$config->addComponent($orderable);

$grid = new GridField(
'TestField',
'TestField',
$parent->MyHasManySubclassOrderedVersioned()->sort('Sort', 'ASC'),
$config
);

$originalOrder = $parent->MyHasManySubclassOrderedVersioned()
->sort('Sort', 'ASC')
->column('ID');

$desiredOrder = [];

// Make order non-contiguous, and 1-based
foreach (array_reverse($originalOrder) as $index => $id) {
$desiredOrder[$index * 2 + 1] = $id;
}

$this->assertNotEquals($originalOrder, $desiredOrder);

$reflection->invoke($orderable, $grid, $desiredOrder);

$newOrder = $parent->MyHasManySubclassOrderedVersioned()
->sort('Sort', 'ASC')
->map('Sort', 'ID')
->toArray();

$this->assertEquals($desiredOrder, $newOrder);

// reorder should have been handled as versioned - there should be a difference between stages now
$differenceFound = false;
foreach ($parent->MyHasManySubclassOrderedVersioned() as $item) {
if ($item->stagesDiffer()) {
$differenceFound = true;
break;
}
}

$this->assertTrue($differenceFound);
}
}
30 changes: 30 additions & 0 deletions tests/OrderableRowsThroughTest.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
Symbiote\GridFieldExtensions\Tests\Stub\ThroughDefiner:
DefinerOne:
DefinerTwo:

Symbiote\GridFieldExtensions\Tests\Stub\ThroughBelongs:
BelongsOne:
BelongsTwo:
BelongsThree:

Symbiote\GridFieldExtensions\Tests\Stub\ThroughIntermediary:
One:
Defining: =>Symbiote\GridFieldExtensions\Tests\Stub\ThroughDefiner.DefinerOne
Belonging: =>Symbiote\GridFieldExtensions\Tests\Stub\ThroughBelongs.BelongsOne
Sort: 3
Two:
Defining: =>Symbiote\GridFieldExtensions\Tests\Stub\ThroughDefiner.DefinerOne
Belonging: =>Symbiote\GridFieldExtensions\Tests\Stub\ThroughBelongs.BelongsTwo
Sort: 2
Three:
Defining: =>Symbiote\GridFieldExtensions\Tests\Stub\ThroughDefiner.DefinerOne
Belonging: =>Symbiote\GridFieldExtensions\Tests\Stub\ThroughBelongs.BelongsThree
Sort: 1
Four:
Defining: =>Symbiote\GridFieldExtensions\Tests\Stub\ThroughDefiner.DefinerTwo
Belonging: =>Symbiote\GridFieldExtensions\Tests\Stub\ThroughBelongs.BelongsTwo
Sort: 1
Five:
Defining: =>Symbiote\GridFieldExtensions\Tests\Stub\ThroughDefiner.DefinerTwo
Belonging: =>Symbiote\GridFieldExtensions\Tests\Stub\ThroughBelongs.BelongsThree
Sort: 2
Loading

0 comments on commit bba8547

Please sign in to comment.