Skip to content

Commit

Permalink
FIX: NestedInputBuilder correctly builds filters for models with mult…
Browse files Browse the repository at this point in the history
…iple relations of the same type (silverstripe#363)

* FIX: NestedInputBuilder correctly builds for repeating connections

* Update NestedInputBuilderTest.php

* CHANGE: test for field lists

* CHANGE: NestedInputBuilder builds all nodes

* CHANGE: Lint

* Update NestedInputBuilder.php

* DELETE: max_nesting config on NestedInputBuilder

* CHANGE: test only checks for type

* FIX: test

* fix

* lint

* ADD: unit test

* relation

* fix test
  • Loading branch information
mleutenegger authored and Aaron Carlino committed Mar 15, 2021
1 parent 7c57265 commit 462aab8
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/Schema/Services/NestedInputBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,10 @@ protected function addInputTypesToSchema(
}
// If we've already seen this input, it can only be added to, not mutated.
if ($inputType->getFieldByName($fieldName)) {
if ($inputType->exists() && $parentType && $parentField) {
$this->schema->addType($inputType);
$parentType->addField($parentField, $inputType->getName());
}
continue;
}

Expand Down
1 change: 1 addition & 0 deletions tests/Fake/FakeProduct.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class FakeProduct extends DataObject implements TestOnly

private static $has_one = [
'Parent' => FakeProductPage::class,
'FeaturedOn' => FakeProductPage::class,
];

private static $has_many = [
Expand Down
3 changes: 2 additions & 1 deletion tests/Fake/FakeProductPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ class FakeProductPage extends DataObject implements TestOnly
private static $table_name = 'FakeProductPage_Test';

private static $has_many = [
'Products' => FakeProduct::class,
'Products' => FakeProduct::class . '.Parent',
'FeaturedProducts' => FakeProduct::class . '.FeaturedOn',
];

private static $owns = [
Expand Down
28 changes: 28 additions & 0 deletions tests/Schema/Services/NestedInputBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,34 @@ public function testNestedInputBuilderBuildsCyclicFilterFields()
$this->assertEquals('MemberFilterFields', $filterFieldObj->getType());
}

/**
* @throws SchemaBuilderException
*/
public function testNestedInputBuilderBuildsRepeatedRelationsFilter()
{
$schema = (new TestSchemaBuilder())->boot('multiConnectionBuilder');
$schema
->addModelbyClassName(FakeProductPage::class, function (ModelType $model) {
$model->addField('title');
$model->addField('products', ['plugins' => ['filter' => true]]);
$model->addField('featuredProducts', ['plugins' => ['filter' => true]]);
})
->addModelbyClassName(FakeProduct::class, function (ModelType $model) {
$model->addField('title');
$model->addField('parent');
$model->addField('featuredOn');
});
$schema->createStoreableSchema();
$filterType = $schema->getType('FakeProductPageFilterFields');
$this->assertNotNull($filterType, "Type FakeProductPageFilterFields not found in schema");
$productsFilter = $filterType->getFieldByName('products');
$this->assertNotNull($productsFilter, "Field products not found on {$filterType->getName()}");
$this->assertEquals('FakeProductFilterFields', $productsFilter->getType());
$featuredFilter = $filterType->getFieldByName('featuredProducts');
$this->assertNotNull($featuredFilter, "Field featuredProducts not found on {$filterType->getName()}");
$this->assertEquals('FakeProductFilterFields', $featuredFilter->getType());
}

private function assertSchema(array $graph, Schema $schema)
{
foreach ($graph as $typeName => $fields) {
Expand Down

0 comments on commit 462aab8

Please sign in to comment.