2012-01-25 23:53:12 +01:00
|
|
|
<?php
|
|
|
|
|
2016-10-14 03:30:05 +02:00
|
|
|
namespace SilverStripe\ORM\Tests;
|
2016-06-15 06:03:16 +02:00
|
|
|
|
2018-11-01 01:42:27 +01:00
|
|
|
use SilverStripe\Core\Config\Config;
|
2016-06-15 06:03:16 +02:00
|
|
|
use SilverStripe\ORM\FieldType\DBMoney;
|
|
|
|
use SilverStripe\ORM\ManyManyList;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\Core\Convert;
|
|
|
|
use SilverStripe\Dev\SapphireTest;
|
2016-10-14 03:30:05 +02:00
|
|
|
use SilverStripe\ORM\Tests\DataObjectTest\Player;
|
|
|
|
use SilverStripe\ORM\Tests\DataObjectTest\Team;
|
2017-06-28 19:23:38 +02:00
|
|
|
use SilverStripe\ORM\Tests\ManyManyListTest\ExtraFieldsObject;
|
|
|
|
use SilverStripe\ORM\Tests\ManyManyListTest\Product;
|
2019-02-26 23:14:47 +01:00
|
|
|
use InvalidArgumentException;
|
2016-08-19 00:51:35 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
class ManyManyListTest extends SapphireTest
|
|
|
|
{
|
|
|
|
|
|
|
|
protected static $fixture_file = 'DataObjectTest.yml';
|
|
|
|
|
|
|
|
public static $extra_data_objects = [
|
|
|
|
ManyManyListTest\Category::class,
|
|
|
|
ManyManyListTest\ExtraFieldsObject::class,
|
|
|
|
ManyManyListTest\Product::class,
|
2019-02-21 23:08:43 +01:00
|
|
|
DataObjectTest\MockDynamicAssignmentDataObject::class
|
2016-12-16 05:34:21 +01:00
|
|
|
];
|
|
|
|
|
2017-06-22 12:50:45 +02:00
|
|
|
public static function getExtraDataObjects()
|
2016-12-16 05:34:21 +01:00
|
|
|
{
|
|
|
|
return array_merge(
|
|
|
|
DataObjectTest::$extra_data_objects,
|
|
|
|
ManyManyListTest::$extra_data_objects
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testAddCompositedExtraFields()
|
|
|
|
{
|
|
|
|
$obj = new ManyManyListTest\ExtraFieldsObject();
|
|
|
|
$obj->write();
|
|
|
|
|
|
|
|
$money = new DBMoney();
|
|
|
|
$money->setAmount(100);
|
|
|
|
$money->setCurrency('USD');
|
|
|
|
|
|
|
|
// the actual test is that this does not generate an error in the sql.
|
|
|
|
$obj->Clients()->add(
|
|
|
|
$obj,
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
2016-12-16 05:34:21 +01:00
|
|
|
'Worth' => $money,
|
|
|
|
'Reference' => 'Foo'
|
2020-04-20 19:58:09 +02:00
|
|
|
]
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
$check = $obj->Clients()->First();
|
|
|
|
|
|
|
|
$this->assertEquals('Foo', $check->Reference, 'Basic scalar fields should exist');
|
|
|
|
$this->assertInstanceOf(DBMoney::class, $check->Worth, 'Composite fields should exist on the record');
|
|
|
|
$this->assertEquals(100, $check->Worth->getAmount());
|
|
|
|
}
|
|
|
|
|
2017-06-28 19:23:38 +02:00
|
|
|
/**
|
|
|
|
* This test targets a bug where appending many_many_extraFields to a query would
|
|
|
|
* result in erroneous queries for sort orders that rely on _SortColumn0
|
|
|
|
*/
|
|
|
|
public function testAddCompositedExtraFieldsWithSortColumn0()
|
|
|
|
{
|
|
|
|
$obj = new ExtraFieldsObject();
|
|
|
|
$obj->write();
|
|
|
|
|
|
|
|
$product = new Product();
|
|
|
|
$product->Title = 'Test Product';
|
|
|
|
$product->write();
|
|
|
|
|
|
|
|
// the actual test is that this does not generate an error in the sql.
|
2020-04-20 19:58:09 +02:00
|
|
|
$obj->Products()->add($product, [
|
2017-06-28 19:23:38 +02:00
|
|
|
'Reference' => 'Foo'
|
2020-04-20 19:58:09 +02:00
|
|
|
]);
|
2017-06-28 19:23:38 +02:00
|
|
|
|
|
|
|
$result = $obj->Products()->First();
|
|
|
|
$this->assertEquals('Foo', $result->Reference, 'Basic scalar fields should exist');
|
|
|
|
$this->assertEquals('Test Product', $result->Title);
|
|
|
|
}
|
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
public function testCreateList()
|
|
|
|
{
|
|
|
|
$list = ManyManyList::create(
|
|
|
|
Team::class,
|
|
|
|
'DataObjectTest_Team_Players',
|
|
|
|
'DataObjectTest_TeamID',
|
|
|
|
'DataObjectTest_PlayerID'
|
|
|
|
);
|
|
|
|
$this->assertEquals(2, $list->count());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function testRelationshipEmptyOnNewRecords()
|
|
|
|
{
|
|
|
|
// Relies on the fact that (unrelated) teams exist in the fixture file already
|
|
|
|
$newPlayer = new Player(); // many_many Teams
|
2020-04-20 19:58:09 +02:00
|
|
|
$this->assertEquals([], $newPlayer->Teams()->column('ID'));
|
2016-12-16 05:34:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testAddingSingleDataObjectByReference()
|
|
|
|
{
|
|
|
|
$player1 = $this->objFromFixture(Player::class, 'player1');
|
|
|
|
$team1 = $this->objFromFixture(Team::class, 'team1');
|
|
|
|
$player1->Teams()->add($team1);
|
|
|
|
$player1->flushCache();
|
|
|
|
|
|
|
|
$compareTeams = new ManyManyList(
|
|
|
|
Team::class,
|
|
|
|
'DataObjectTest_Team_Players',
|
|
|
|
'DataObjectTest_TeamID',
|
|
|
|
'DataObjectTest_PlayerID'
|
|
|
|
);
|
|
|
|
$compareTeams = $compareTeams->forForeignID($player1->ID);
|
|
|
|
$this->assertEquals(
|
|
|
|
$player1->Teams()->column('ID'),
|
|
|
|
$compareTeams->column('ID'),
|
|
|
|
"Adding single record as DataObject to many_many"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testRemovingSingleDataObjectByReference()
|
|
|
|
{
|
|
|
|
$player1 = $this->objFromFixture(Player::class, 'player1');
|
|
|
|
$team1 = $this->objFromFixture(Team::class, 'team1');
|
|
|
|
$player1->Teams()->remove($team1);
|
|
|
|
$player1->flushCache();
|
|
|
|
$compareTeams = new ManyManyList(
|
|
|
|
Team::class,
|
|
|
|
'DataObjectTest_Team_Players',
|
|
|
|
'DataObjectTest_TeamID',
|
|
|
|
'DataObjectTest_PlayerID'
|
|
|
|
);
|
|
|
|
$compareTeams = $compareTeams->forForeignID($player1->ID);
|
|
|
|
$this->assertEquals(
|
|
|
|
$player1->Teams()->column('ID'),
|
|
|
|
$compareTeams->column('ID'),
|
|
|
|
"Removing single record as DataObject from many_many"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testAddingSingleDataObjectByID()
|
|
|
|
{
|
|
|
|
$player1 = $this->objFromFixture(Player::class, 'player1');
|
|
|
|
$team1 = $this->objFromFixture(Team::class, 'team1');
|
|
|
|
$player1->Teams()->add($team1->ID);
|
|
|
|
$player1->flushCache();
|
|
|
|
$compareTeams = new ManyManyList(
|
|
|
|
Team::class,
|
|
|
|
'DataObjectTest_Team_Players',
|
|
|
|
'DataObjectTest_TeamID',
|
|
|
|
'DataObjectTest_PlayerID'
|
|
|
|
);
|
|
|
|
$compareTeams = $compareTeams->forForeignID($player1->ID);
|
|
|
|
$this->assertEquals(
|
|
|
|
$player1->Teams()->column('ID'),
|
|
|
|
$compareTeams->column('ID'),
|
|
|
|
"Adding single record as ID to many_many"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testRemoveByID()
|
|
|
|
{
|
|
|
|
$player1 = $this->objFromFixture(Player::class, 'player1');
|
|
|
|
$team1 = $this->objFromFixture(Team::class, 'team1');
|
|
|
|
$player1->Teams()->removeByID($team1->ID);
|
|
|
|
$player1->flushCache();
|
|
|
|
$compareTeams = new ManyManyList(
|
|
|
|
Team::class,
|
|
|
|
'DataObjectTest_Team_Players',
|
|
|
|
'DataObjectTest_TeamID',
|
|
|
|
'DataObjectTest_PlayerID'
|
|
|
|
);
|
|
|
|
$compareTeams = $compareTeams->forForeignID($player1->ID);
|
|
|
|
$this->assertEquals(
|
|
|
|
$player1->Teams()->column('ID'),
|
|
|
|
$compareTeams->column('ID'),
|
|
|
|
"Removing single record as ID from many_many"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSetByIdList()
|
|
|
|
{
|
|
|
|
$player1 = $this->objFromFixture(Player::class, 'player1');
|
|
|
|
$team1 = $this->objFromFixture(Team::class, 'team1');
|
|
|
|
$team2 = $this->objFromFixture(Team::class, 'team2');
|
2020-04-20 19:58:09 +02:00
|
|
|
$player1->Teams()->setByIdList([$team1->ID, $team2->ID]);
|
|
|
|
$this->assertEquals([$team1->ID, $team2->ID], $player1->Teams()->sort('Title')->column());
|
|
|
|
$player1->Teams()->setByIdList([$team1->ID]);
|
|
|
|
$this->assertEquals([$team1->ID], $player1->Teams()->sort('Title')->column());
|
|
|
|
$player1->Teams()->setByIdList([$team2->ID]);
|
|
|
|
$this->assertEquals([$team2->ID], $player1->Teams()->sort('Title')->column());
|
2016-12-16 05:34:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testAddingWithMultipleForeignKeys()
|
|
|
|
{
|
|
|
|
$newPlayer = new Player();
|
|
|
|
$newPlayer->write();
|
|
|
|
$team1 = $this->objFromFixture(Team::class, 'team1');
|
|
|
|
$team2 = $this->objFromFixture(Team::class, 'team2');
|
|
|
|
|
|
|
|
$playersTeam1Team2 = Team::get()->relation('Players')
|
2020-04-20 19:58:09 +02:00
|
|
|
->forForeignID([$team1->ID, $team2->ID]);
|
2016-12-16 05:34:21 +01:00
|
|
|
$playersTeam1Team2->add($newPlayer);
|
|
|
|
$this->assertEquals(
|
2020-04-20 19:58:09 +02:00
|
|
|
[$team1->ID, $team2->ID],
|
2016-12-16 05:34:21 +01:00
|
|
|
$newPlayer->Teams()->sort('Title')->column('ID')
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testAddingExistingDoesntRemoveExtraFields()
|
|
|
|
{
|
|
|
|
$player = new Player();
|
|
|
|
$player->write();
|
|
|
|
$team1 = $this->objFromFixture(Team::class, 'team1');
|
|
|
|
|
2020-04-20 19:58:09 +02:00
|
|
|
$team1->Players()->add($player, ['Position' => 'Captain']);
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertEquals(
|
2020-04-20 19:58:09 +02:00
|
|
|
['Position' => 'Captain'],
|
2016-12-16 05:34:21 +01:00
|
|
|
$team1->Players()->getExtraData('Teams', $player->ID),
|
|
|
|
'Writes extrafields'
|
|
|
|
);
|
|
|
|
|
|
|
|
$team1->Players()->add($player);
|
|
|
|
$this->assertEquals(
|
2020-04-20 19:58:09 +02:00
|
|
|
['Position' => 'Captain'],
|
2016-12-16 05:34:21 +01:00
|
|
|
$team1->Players()->getExtraData('Teams', $player->ID),
|
|
|
|
'Retains extrafields on subsequent adds with NULL fields'
|
|
|
|
);
|
|
|
|
|
2020-04-20 19:58:09 +02:00
|
|
|
$team1->Players()->add($player, ['Position' => 'Defense']);
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertEquals(
|
2020-04-20 19:58:09 +02:00
|
|
|
['Position' => 'Defense'],
|
2016-12-16 05:34:21 +01:00
|
|
|
$team1->Players()->getExtraData('Teams', $player->ID),
|
|
|
|
'Updates extrafields on subsequent adds with fields'
|
|
|
|
);
|
|
|
|
|
2020-04-20 19:58:09 +02:00
|
|
|
$team1->Players()->add($player, ['Position' => null]);
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertEquals(
|
2020-04-20 19:58:09 +02:00
|
|
|
['Position' => null],
|
2016-12-16 05:34:21 +01:00
|
|
|
$team1->Players()->getExtraData('Teams', $player->ID),
|
|
|
|
'Allows clearing of extrafields on subsequent adds'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSubtractOnAManyManyList()
|
|
|
|
{
|
|
|
|
$allList = ManyManyList::create(
|
|
|
|
Player::class,
|
|
|
|
'DataObjectTest_Team_Players',
|
|
|
|
'DataObjectTest_PlayerID',
|
|
|
|
'DataObjectTest_TeamID'
|
|
|
|
);
|
|
|
|
$this->assertEquals(
|
|
|
|
3,
|
|
|
|
$allList->count(),
|
|
|
|
'Precondition; we have all 3 players connected to a team in the list'
|
|
|
|
);
|
|
|
|
|
|
|
|
$teamOneID = $this->idFromFixture(Team::class, 'team1');
|
|
|
|
$teamTwoID = $this->idFromFixture(Team::class, 'team2');
|
|
|
|
|
|
|
|
// Captain 1 belongs to one team; team1
|
|
|
|
$captain1 = $this->objFromFixture(Player::class, 'captain1');
|
|
|
|
$this->assertEquals(
|
2020-04-20 19:58:09 +02:00
|
|
|
[$teamOneID],
|
2016-12-16 05:34:21 +01:00
|
|
|
$captain1->Teams()->column("ID"),
|
|
|
|
'Precondition; player2 belongs to team1'
|
|
|
|
);
|
|
|
|
|
|
|
|
// Player 2 belongs to both teams: team1, team2
|
|
|
|
$player2 = $this->objFromFixture(Player::class, 'player2');
|
|
|
|
$this->assertEquals(
|
2020-04-20 19:58:09 +02:00
|
|
|
[$teamOneID,$teamTwoID],
|
2016-12-16 05:34:21 +01:00
|
|
|
$player2->Teams()->sort('Title')->column('ID'),
|
|
|
|
'Precondition; player2 belongs to team1 and team2'
|
|
|
|
);
|
|
|
|
|
|
|
|
// We want to find the teams for player2 where the captain does not belong to
|
|
|
|
$teamsWithoutTheCaptain = $player2->Teams()->subtract($captain1->Teams());
|
|
|
|
|
|
|
|
// Assertions
|
|
|
|
$this->assertEquals(
|
|
|
|
1,
|
|
|
|
$teamsWithoutTheCaptain->count(),
|
|
|
|
'The ManyManyList should onlu contain one team'
|
|
|
|
);
|
|
|
|
$this->assertEquals(
|
|
|
|
$teamTwoID,
|
|
|
|
$teamsWithoutTheCaptain->first()->ID,
|
|
|
|
'The ManyManyList contains the wrong team'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testRemoveAll()
|
|
|
|
{
|
|
|
|
$first = new Team();
|
|
|
|
$first->write();
|
|
|
|
|
|
|
|
$second = new Team();
|
|
|
|
$second->write();
|
|
|
|
|
|
|
|
$firstPlayers = $first->Players();
|
|
|
|
$secondPlayers = $second->Players();
|
|
|
|
|
|
|
|
$a = new Player();
|
|
|
|
$a->ShirtNumber = 'a';
|
|
|
|
$a->write();
|
|
|
|
|
|
|
|
$b = new Player();
|
|
|
|
$b->ShirtNumber = 'b';
|
|
|
|
$b->write();
|
|
|
|
|
|
|
|
$firstPlayers->add($a);
|
|
|
|
$firstPlayers->add($b);
|
|
|
|
|
|
|
|
$secondPlayers->add($a);
|
|
|
|
$secondPlayers->add($b);
|
|
|
|
|
2020-04-20 19:58:09 +02:00
|
|
|
$this->assertEquals(['a', 'b'], $firstPlayers->sort('ShirtNumber')->column('ShirtNumber'));
|
|
|
|
$this->assertEquals(['a', 'b'], $secondPlayers->sort('ShirtNumber')->column('ShirtNumber'));
|
2016-12-16 05:34:21 +01:00
|
|
|
|
|
|
|
$firstPlayers->removeAll();
|
|
|
|
|
2022-04-14 03:12:59 +02:00
|
|
|
$this->assertEquals(0, count($firstPlayers ?? []));
|
|
|
|
$this->assertEquals(2, count($secondPlayers ?? []));
|
2016-12-16 05:34:21 +01:00
|
|
|
|
|
|
|
$firstPlayers->removeAll();
|
|
|
|
|
|
|
|
$firstPlayers->add($a);
|
|
|
|
$firstPlayers->add($b);
|
|
|
|
|
2020-04-20 19:58:09 +02:00
|
|
|
$this->assertEquals(['a', 'b'], $firstPlayers->sort('ShirtNumber')->column('ShirtNumber'));
|
2016-12-16 05:34:21 +01:00
|
|
|
|
|
|
|
$firstPlayers->filter('ShirtNumber', 'b')->removeAll();
|
|
|
|
|
2020-04-20 19:58:09 +02:00
|
|
|
$this->assertEquals(['a'], $firstPlayers->column('ShirtNumber'));
|
|
|
|
$this->assertEquals(['a', 'b'], $secondPlayers->sort('ShirtNumber')->column('ShirtNumber'));
|
2016-12-16 05:34:21 +01:00
|
|
|
|
|
|
|
$this->assertNotNull(Player::get()->byID($a->ID));
|
|
|
|
$this->assertNotNull(Player::get()->byID($b->ID));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testAppendExtraFieldsToQuery()
|
|
|
|
{
|
|
|
|
$list = new ManyManyList(
|
|
|
|
ManyManyListTest\ExtraFieldsObject::class,
|
|
|
|
'ManyManyListTest_ExtraFields_Clients',
|
|
|
|
'ManyManyListTest_ExtraFieldsID',
|
|
|
|
'ChildID',
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
2016-12-16 05:34:21 +01:00
|
|
|
'Worth' => 'Money',
|
|
|
|
'Reference' => 'Varchar'
|
2020-04-20 19:58:09 +02:00
|
|
|
]
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
// ensure that ManyManyListTest_ExtraFields_Clients.ValueCurrency is
|
|
|
|
// selected.
|
|
|
|
$expected = 'SELECT DISTINCT "ManyManyListTest_ExtraFields_Clients"."WorthCurrency",'
|
2018-01-16 19:39:30 +01:00
|
|
|
. ' "ManyManyListTest_ExtraFields_Clients"."WorthAmount", "ManyManyListTest_ExtraFields_Clients"."Reference",'
|
|
|
|
. ' "ManyManyListTest_ExtraFields"."ClassName", "ManyManyListTest_ExtraFields"."LastEdited",'
|
|
|
|
. ' "ManyManyListTest_ExtraFields"."Created", "ManyManyListTest_ExtraFields"."ID",'
|
|
|
|
. ' CASE WHEN "ManyManyListTest_ExtraFields"."ClassName" IS NOT NULL THEN'
|
|
|
|
. ' "ManyManyListTest_ExtraFields"."ClassName" ELSE ' . Convert::raw2sql(ManyManyListTest\ExtraFieldsObject::class, true)
|
|
|
|
. ' END AS "RecordClassName" FROM "ManyManyListTest_ExtraFields" INNER JOIN'
|
|
|
|
. ' "ManyManyListTest_ExtraFields_Clients" ON'
|
|
|
|
. ' "ManyManyListTest_ExtraFields_Clients"."ManyManyListTest_ExtraFieldsID" ='
|
|
|
|
. ' "ManyManyListTest_ExtraFields"."ID"';
|
2016-12-16 05:34:21 +01:00
|
|
|
|
|
|
|
$this->assertSQLEquals($expected, $list->sql($parameters));
|
|
|
|
}
|
|
|
|
|
2018-11-01 01:42:27 +01:00
|
|
|
/**
|
|
|
|
* This tests that we can set a default sort on a join table, even though the class doesn't exist.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testSortByExtraFieldsDefaultSort()
|
|
|
|
{
|
|
|
|
$obj = new ManyManyListTest\ExtraFieldsObject();
|
|
|
|
$obj->write();
|
|
|
|
|
|
|
|
$obj2 = new ManyManyListTest\ExtraFieldsObject();
|
|
|
|
$obj2->write();
|
|
|
|
|
|
|
|
$money = new DBMoney();
|
|
|
|
$money->setAmount(100);
|
|
|
|
$money->setCurrency('USD');
|
|
|
|
|
|
|
|
// Add two objects as relations (first is linking back to itself)
|
|
|
|
$obj->Clients()->add($obj, ['Worth' => $money, 'Reference' => 'A']);
|
|
|
|
$obj->Clients()->add($obj2, ['Worth' => $money, 'Reference' => 'B']);
|
|
|
|
|
|
|
|
// Set the default sort for this relation
|
|
|
|
Config::inst()->update('ManyManyListTest_ExtraFields_Clients', 'default_sort', 'Reference ASC');
|
|
|
|
$clients = $obj->Clients();
|
|
|
|
$this->assertCount(2, $clients);
|
|
|
|
|
|
|
|
list($first, $second) = $obj->Clients();
|
|
|
|
$this->assertEquals('A', $first->Reference);
|
|
|
|
$this->assertEquals('B', $second->Reference);
|
|
|
|
|
|
|
|
// Now we ensure the default sort is being respected by reversing its order
|
|
|
|
Config::inst()->update('ManyManyListTest_ExtraFields_Clients', 'default_sort', 'Reference DESC');
|
|
|
|
$reverseClients = $obj->Clients();
|
|
|
|
$this->assertCount(2, $reverseClients);
|
|
|
|
|
|
|
|
list($reverseFirst, $reverseSecond) = $obj->Clients();
|
|
|
|
$this->assertEquals('B', $reverseFirst->Reference);
|
|
|
|
$this->assertEquals('A', $reverseSecond->Reference);
|
|
|
|
}
|
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
public function testFilteringOnPreviouslyJoinedTable()
|
|
|
|
{
|
2017-05-19 04:07:45 +02:00
|
|
|
/** @var ManyManyListTest\Category $category */
|
2016-12-16 05:34:21 +01:00
|
|
|
$category = $this->objFromFixture(ManyManyListTest\Category::class, 'categorya');
|
|
|
|
|
2017-05-19 04:07:45 +02:00
|
|
|
/** @var ManyManyList $productsRelatedToProductB */
|
|
|
|
$productsRelatedToProductB = $category->Products()->filter('RelatedProducts.Title', 'Product A');
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertEquals(1, $productsRelatedToProductB->count());
|
|
|
|
}
|
2019-02-21 23:08:43 +01:00
|
|
|
|
|
|
|
public function testWriteManipulationWithNonScalarValuesAllowed()
|
|
|
|
{
|
|
|
|
$left = DataObjectTest\MockDynamicAssignmentDataObject::create();
|
|
|
|
$left->write();
|
|
|
|
$right = DataObjectTest\MockDynamicAssignmentDataObject::create();
|
|
|
|
$right->write();
|
|
|
|
|
|
|
|
$left->MockManyMany()->add($right, [
|
|
|
|
'ManyManyStaticScalarOnlyField' => true,
|
|
|
|
'ManyManyDynamicScalarOnlyField' => false,
|
|
|
|
'ManyManyDynamicField' => true,
|
|
|
|
]);
|
|
|
|
|
|
|
|
$pivot = $left->MockManyMany()->first();
|
|
|
|
|
|
|
|
$this->assertNotFalse($pivot->ManyManyStaticScalarOnlyField);
|
|
|
|
$this->assertNotTrue($pivot->ManyManyDynamicScalarOnlyField);
|
|
|
|
$this->assertNotFalse($pivot->ManyManyDynamicField);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testWriteManipulationWithNonScalarValuesDisallowed()
|
|
|
|
{
|
2021-10-27 04:39:47 +02:00
|
|
|
$this->expectException(\InvalidArgumentException::class);
|
2019-02-21 23:08:43 +01:00
|
|
|
|
|
|
|
$left = DataObjectTest\MockDynamicAssignmentDataObject::create();
|
|
|
|
$left->write();
|
|
|
|
$right = DataObjectTest\MockDynamicAssignmentDataObject::create();
|
|
|
|
$right->write();
|
|
|
|
|
|
|
|
$left->MockManyMany()->add($right, [
|
|
|
|
'ManyManyStaticScalarOnlyField' => false,
|
|
|
|
'ManyManyDynamicScalarOnlyField' => true,
|
|
|
|
'ManyManyDynamicField' => false,
|
|
|
|
]);
|
|
|
|
}
|
2020-07-02 04:44:27 +02:00
|
|
|
|
|
|
|
public function testCallbackOnSetById()
|
|
|
|
{
|
|
|
|
$addedIds = [];
|
|
|
|
$removedIds = [];
|
|
|
|
|
|
|
|
$base = $this->objFromFixture(Team::class, 'team1');
|
|
|
|
$relation = $base->Players();
|
|
|
|
$remove = $relation->First();
|
|
|
|
$add = new Player();
|
|
|
|
$add->write();
|
|
|
|
|
|
|
|
$relation->addCallbacks()->add(function ($list, $item, $extraFields) use (&$removedIds) {
|
|
|
|
$addedIds[] = $item;
|
|
|
|
});
|
|
|
|
|
|
|
|
$relation->removeCallbacks()->add(function ($list, $ids) use (&$removedIds) {
|
|
|
|
$removedIds = $ids;
|
|
|
|
});
|
|
|
|
|
|
|
|
$relation->setByIDList(array_merge(
|
|
|
|
$base->Players()->exclude('ID', $remove->ID)->column('ID'),
|
|
|
|
[$add->ID]
|
|
|
|
));
|
|
|
|
$this->assertEquals([$remove->ID], $removedIds);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testAddCallbackWithExtraFields()
|
|
|
|
{
|
|
|
|
$added = [];
|
|
|
|
|
|
|
|
$base = $this->objFromFixture(Team::class, 'team1');
|
|
|
|
$relation = $base->Players();
|
|
|
|
$add = new Player();
|
|
|
|
$add->write();
|
|
|
|
|
|
|
|
$relation->addCallbacks()->add(function ($list, $item, $extraFields) use (&$added) {
|
|
|
|
$added[] = [$item, $extraFields];
|
|
|
|
});
|
|
|
|
|
|
|
|
$relation->add($add, ['Position' => 'Quarterback']);
|
|
|
|
$this->assertEquals([[$add, ['Position' => 'Quarterback']]], $added);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testRemoveCallbackOnRemove()
|
|
|
|
{
|
|
|
|
$removedIds = [];
|
|
|
|
|
|
|
|
$base = $this->objFromFixture(Team::class, 'team1');
|
|
|
|
$relation = $base->Players();
|
|
|
|
$remove = $relation->First();
|
|
|
|
|
|
|
|
$relation->removeCallbacks()->add(function ($list, $ids) use (&$removedIds) {
|
|
|
|
$removedIds = $ids;
|
|
|
|
});
|
|
|
|
|
|
|
|
$relation->remove($remove);
|
|
|
|
$this->assertEquals([$remove->ID], $removedIds);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testRemoveCallbackOnRemoveById()
|
|
|
|
{
|
|
|
|
$removedIds = [];
|
|
|
|
|
|
|
|
$base = $this->objFromFixture(Team::class, 'team1');
|
|
|
|
$relation = $base->Players();
|
|
|
|
$remove = $relation->First();
|
|
|
|
|
|
|
|
$relation->removeCallbacks()->add(function ($list, $ids) use (&$removedIds) {
|
|
|
|
$removedIds = $ids;
|
|
|
|
});
|
|
|
|
|
|
|
|
$relation->removeByID($remove->ID);
|
|
|
|
$this->assertEquals([$remove->ID], $removedIds);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testRemoveCallbackOnRemoveAll()
|
|
|
|
{
|
|
|
|
$removedIds = [];
|
|
|
|
|
|
|
|
$base = $this->objFromFixture(Team::class, 'team1');
|
|
|
|
$relation = $base->Players();
|
|
|
|
$remove = $relation->column('ID');
|
|
|
|
|
|
|
|
$relation->removeCallbacks()->add(function ($list, $ids) use (&$removedIds) {
|
|
|
|
$removedIds = $ids;
|
|
|
|
});
|
|
|
|
|
|
|
|
$relation->removeAll();
|
|
|
|
$this->assertEquals(sort($remove), sort($removedIds));
|
|
|
|
}
|
2014-06-28 00:29:07 +02:00
|
|
|
}
|