Skip to content

Commit a507079

Browse files
committed
Finish test coverage with data provider
1 parent 937667d commit a507079

File tree

1 file changed

+41
-4
lines changed

1 file changed

+41
-4
lines changed

tests/src/Kernel/DataProducer/DefaultValueTest.php

+41-4
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
use Drupal\Core\Session\AccountInterface;
66
use Drupal\graphql\GraphQL\Execution\FieldContext;
77
use Drupal\graphql\Plugin\GraphQL\DataProducer\Entity\EntityLoad;
8-
use Drupal\media_test_source\Plugin\media\Source\Test;
98
use Drupal\Tests\graphql\Kernel\GraphQLTestBase;
109
use GraphQL\Deferred;
1110
use PHPUnit\Framework\Assert;
@@ -32,16 +31,18 @@ public function testEntityLoadDefaultValue(): void {
3231

3332
/**
3433
* Test that the legacy dataproducer_populate_default_values setting works.
34+
*
35+
* @dataProvider settingsProvider
3536
*/
36-
public function testLegacyDefaultValueSetting(): void {
37+
public function testLegacyDefaultValueSetting(bool $populate_setting, string $testClass): void {
3738
$this->container->get('config.factory')->getEditable('graphql.settings')
38-
->set('dataproducer_populate_default_values', FALSE)
39+
->set('dataproducer_populate_default_values', $populate_setting)
3940
->save();
4041
$manager = $this->container->get('plugin.manager.graphql.data_producer');
4142

4243
// Manipulate the plugin definitions to use our test class for entity_load.
4344
$definitions = $manager->getDefinitions();
44-
$definitions['entity_load']['class'] = TestLegacyEntityLoad::class;
45+
$definitions['entity_load']['class'] = $testClass;
4546
$reflection = new \ReflectionClass($manager);
4647
$property = $reflection->getProperty('definitions');
4748
$property->setAccessible(TRUE);
@@ -50,12 +51,48 @@ public function testLegacyDefaultValueSetting(): void {
5051
$this->executeDataProducer('entity_load', ['type' => 'node']);
5152
}
5253

54+
/**
55+
* Data provider for the testLegacyDefaultValueSetting test.
56+
*/
57+
public function settingsProvider(): array {
58+
return [
59+
[FALSE, TestLegacyEntityLoad::class],
60+
[TRUE, TestNewEntityLoad::class],
61+
];
62+
}
63+
5364
}
5465

66+
/**
67+
* Helper class to test the legacy behavior.
68+
*/
5569
class TestLegacyEntityLoad extends EntityLoad {
70+
71+
/**
72+
* {@inheritdoc}
73+
*/
5674
public function resolve($type, $id, ?string $language, ?array $bundles, ?bool $access, ?AccountInterface $accessUser, ?string $accessOperation, FieldContext $context): ?Deferred {
75+
// Old behavior: no default values applied, so we get NULL here.
5776
Assert::assertNull($access);
5877
Assert::assertNull($accessOperation);
5978
return NULL;
6079
}
80+
81+
}
82+
83+
/**
84+
* Helper class to test the new behavior.
85+
*/
86+
class TestNewEntityLoad extends EntityLoad {
87+
88+
/**
89+
* {@inheritdoc}
90+
*/
91+
public function resolve($type, $id, ?string $language, ?array $bundles, ?bool $access, ?AccountInterface $accessUser, ?string $accessOperation, FieldContext $context): ?Deferred {
92+
// New behavior: default values are applied.
93+
Assert::assertTrue($access);
94+
Assert::assertSame('view', $accessOperation);
95+
return NULL;
96+
}
97+
6198
}

0 commit comments

Comments
 (0)