-
Notifications
You must be signed in to change notification settings - Fork 125
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add unit tests for new ManyManyThrough support
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
Jun 25, 2018
1 parent
9fa9ef8
commit 08ed6f6
Showing
8 changed files
with
270 additions
and
29 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
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,40 @@ | ||
Symbiote\GridFieldExtensions\Tests\Stub\ThroughDefiner: | ||
DefinerOne: | ||
Title: 'One' | ||
DefinerTwo: | ||
Title: 'Two' | ||
|
||
Symbiote\GridFieldExtensions\Tests\Stub\ThroughBelongs: | ||
BelongsOne: | ||
Title: 'One' | ||
BelongsTwo: | ||
Title: 'Two' | ||
BelongsThree: | ||
Title: 'Three' | ||
|
||
Symbiote\GridFieldExtensions\Tests\Stub\ThroughIntermediary: | ||
One: | ||
Title: 'One' | ||
Defining: =>Symbiote\GridFieldExtensions\Tests\Stub\ThroughDefiner.DefinerOne | ||
Belonging: =>Symbiote\GridFieldExtensions\Tests\Stub\ThroughBelongs.BelongsOne | ||
Sort: 3 | ||
Two: | ||
Title: 'Two' | ||
Defining: =>Symbiote\GridFieldExtensions\Tests\Stub\ThroughDefiner.DefinerOne | ||
Belonging: =>Symbiote\GridFieldExtensions\Tests\Stub\ThroughBelongs.BelongsTwo | ||
Sort: 2 | ||
Three: | ||
Title: 'Three' | ||
Defining: =>Symbiote\GridFieldExtensions\Tests\Stub\ThroughDefiner.DefinerOne | ||
Belonging: =>Symbiote\GridFieldExtensions\Tests\Stub\ThroughBelongs.BelongsThree | ||
Sort: 1 | ||
Four: | ||
Title: 'Four' | ||
Defining: =>Symbiote\GridFieldExtensions\Tests\Stub\ThroughDefiner.DefinerTwo | ||
Belonging: =>Symbiote\GridFieldExtensions\Tests\Stub\ThroughBelongs.BelongsTwo | ||
Sort: 1 | ||
Five: | ||
Title: 'Five' | ||
Defining: =>Symbiote\GridFieldExtensions\Tests\Stub\ThroughDefiner.DefinerTwo | ||
Belonging: =>Symbiote\GridFieldExtensions\Tests\Stub\ThroughBelongs.BelongsThree | ||
Sort: 2 |
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,109 @@ | ||
<?php | ||
|
||
namespace Symbiote\GridFieldExtensions\Tests; | ||
|
||
use ReflectionMethod; | ||
use SilverStripe\Dev\SapphireTest; | ||
use SilverStripe\Versioned\Versioned; | ||
use SilverStripe\Forms\GridField\GridField; | ||
use SilverStripe\Forms\GridField\GridFieldConfig_RelationEditor; | ||
use Symbiote\GridFieldExtensions\Tests\Stub\ThroughDefiner; | ||
use Symbiote\GridFieldExtensions\Tests\Stub\ThroughIntermediary; | ||
use Symbiote\GridFieldExtensions\Tests\Stub\ThroughBelongs; | ||
use Symbiote\GridFieldExtensions\GridFieldOrderableRows; | ||
|
||
class OrderableRowsThroughTest extends SapphireTest | ||
{ | ||
protected static $fixture_file = 'OrderableRowsThroughTest.yml'; | ||
|
||
protected static $extra_dataobjects = [ | ||
ThroughDefiner::class, | ||
ThroughIntermediary::class, | ||
ThroughBelongs::class, | ||
]; | ||
|
||
protected static $required_extensions = [ | ||
ThroughDefiner::class => [Versioned::class], | ||
ThroughIntermediary::class => [Versioned::class], | ||
ThroughBelongs::class => [Versioned::class], | ||
]; | ||
|
||
/** | ||
* Mostly the same as GridFieldOrderableRowsTest::testReorderItems | ||
* but with some Versioned calls & checks mixed in. | ||
*/ | ||
public function testReorderingSaves() | ||
{ | ||
$parent = $this->objFromFixture(ThroughDefiner::class, 'DefinerOne'); | ||
$sortName = 'Sort'; | ||
|
||
$orderable = new GridFieldOrderableRows($sortName); | ||
$reflection = new ReflectionMethod($orderable, 'executeReorder'); | ||
$reflection->setAccessible(true); | ||
|
||
$config = new GridFieldConfig_RelationEditor(); | ||
$config->addComponent($orderable); | ||
|
||
// This test data is versioned - ensure we're all published | ||
$parent->publishRecursive(); | ||
// there should be no difference between stages at this point | ||
foreach ($parent->Belongings() as $item) { | ||
$this->assertFalse( | ||
$item->stagesDiffer(), | ||
'No records should be different from their published versions' | ||
); | ||
} | ||
|
||
$grid = new GridField( | ||
'Belongings', | ||
'Testing Many Many', | ||
$parent->Belongings()->sort($sortName), | ||
$config | ||
); | ||
|
||
$originalOrder = $parent->Belongings()->sort($sortName)->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->Belongings()->sort($sortName)->map($sortName, 'ID')->toArray(); | ||
|
||
$this->assertEquals($desiredOrder, $newOrder); | ||
|
||
// pass forward to testReorderedPublish | ||
return $parent; | ||
} | ||
|
||
/** | ||
* @depends testReorderingSaves | ||
* @param ThroughDefiner $parent passed through from testReorderingSaves | ||
*/ | ||
public function testReorderedPublish($parent) | ||
{ | ||
// reorder should have been handled as versioned - there should be a difference between stages now | ||
$differenceFound = false; | ||
foreach ($parent->Belongings() as $item) { | ||
if ($item->stagesDiffer()) { | ||
$differenceFound = true; | ||
break; | ||
} | ||
} | ||
$this->assertTrue($differenceFound, 'At least one record should be changed'); | ||
|
||
$parent->publishRecursive(); | ||
|
||
foreach ($parent->Belongings() as $item) { | ||
$this->assertFalse( | ||
$item->stagesDiffer(), | ||
'No records should be different from their published versions anymore' | ||
); | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
namespace Symbiote\GridFieldExtensions\Tests\Stub; | ||
|
||
use SilverStripe\ORM\DataObject; | ||
use SilverStripe\Dev\TestOnly; | ||
|
||
class ThroughBelongs extends DataObject implements TestOnly | ||
{ | ||
private static $table_name = 'BelongsThrough'; | ||
|
||
private static $db = [ | ||
'Title' => 'Varchar', | ||
]; | ||
|
||
private static $belongs_many_many = [ | ||
'Definers' => ThroughDefiner::class, | ||
]; | ||
} |
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,27 @@ | ||
<?php | ||
|
||
namespace Symbiote\GridFieldExtensions\Tests\Stub; | ||
|
||
use SilverStripe\ORM\DataObject; | ||
use SilverStripe\Dev\TestOnly; | ||
|
||
class ThroughDefiner extends DataObject implements TestOnly | ||
{ | ||
private static $table_name = 'ManyThrough'; | ||
|
||
private static $db = [ | ||
'Title' => 'Varchar', | ||
]; | ||
|
||
private static $many_many = [ | ||
'Belongings' => [ | ||
'through' => ThroughIntermediary::class, | ||
'from' => 'Defining', | ||
'to' => 'Belonging', | ||
] | ||
]; | ||
|
||
private static $owns = [ | ||
'Belongings' | ||
]; | ||
} |
Oops, something went wrong.