5
5
use Drupal \Core \Session \AccountInterface ;
6
6
use Drupal \graphql \GraphQL \Execution \FieldContext ;
7
7
use Drupal \graphql \Plugin \GraphQL \DataProducer \Entity \EntityLoad ;
8
- use Drupal \media_test_source \Plugin \media \Source \Test ;
9
8
use Drupal \Tests \graphql \Kernel \GraphQLTestBase ;
10
9
use GraphQL \Deferred ;
11
10
use PHPUnit \Framework \Assert ;
@@ -32,16 +31,18 @@ public function testEntityLoadDefaultValue(): void {
32
31
33
32
/**
34
33
* Test that the legacy dataproducer_populate_default_values setting works.
34
+ *
35
+ * @dataProvider settingsProvider
35
36
*/
36
- public function testLegacyDefaultValueSetting (): void {
37
+ public function testLegacyDefaultValueSetting (bool $ populate_setting , string $ testClass ): void {
37
38
$ this ->container ->get ('config.factory ' )->getEditable ('graphql.settings ' )
38
- ->set ('dataproducer_populate_default_values ' , FALSE )
39
+ ->set ('dataproducer_populate_default_values ' , $ populate_setting )
39
40
->save ();
40
41
$ manager = $ this ->container ->get ('plugin.manager.graphql.data_producer ' );
41
42
42
43
// Manipulate the plugin definitions to use our test class for entity_load.
43
44
$ definitions = $ manager ->getDefinitions ();
44
- $ definitions ['entity_load ' ]['class ' ] = TestLegacyEntityLoad::class ;
45
+ $ definitions ['entity_load ' ]['class ' ] = $ testClass ;
45
46
$ reflection = new \ReflectionClass ($ manager );
46
47
$ property = $ reflection ->getProperty ('definitions ' );
47
48
$ property ->setAccessible (TRUE );
@@ -50,12 +51,48 @@ public function testLegacyDefaultValueSetting(): void {
50
51
$ this ->executeDataProducer ('entity_load ' , ['type ' => 'node ' ]);
51
52
}
52
53
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
+
53
64
}
54
65
66
+ /**
67
+ * Helper class to test the legacy behavior.
68
+ */
55
69
class TestLegacyEntityLoad extends EntityLoad {
70
+
71
+ /**
72
+ * {@inheritdoc}
73
+ */
56
74
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.
57
76
Assert::assertNull ($ access );
58
77
Assert::assertNull ($ accessOperation );
59
78
return NULL ;
60
79
}
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
+
61
98
}
0 commit comments