|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Drupal\Tests\graphql\Kernel\DataProducer; |
| 4 | + |
| 5 | +use Drupal\Core\Session\AccountInterface; |
| 6 | +use Drupal\graphql\GraphQL\Execution\FieldContext; |
| 7 | +use Drupal\graphql\Plugin\GraphQL\DataProducer\Entity\EntityLoad; |
| 8 | +use Drupal\Tests\graphql\Kernel\GraphQLTestBase; |
| 9 | +use GraphQL\Deferred; |
| 10 | +use PHPUnit\Framework\Assert; |
| 11 | + |
| 12 | +/** |
| 13 | + * Context default value test. |
| 14 | + * |
| 15 | + * @group graphql |
| 16 | + */ |
| 17 | +class DefaultValueTest extends GraphQLTestBase { |
| 18 | + |
| 19 | + /** |
| 20 | + * Test that the entity_load data producer has the correct default values. |
| 21 | + */ |
| 22 | + public function testEntityLoadDefaultValue(): void { |
| 23 | + $manager = $this->container->get('plugin.manager.graphql.data_producer'); |
| 24 | + $plugin = $manager->createInstance('entity_load'); |
| 25 | + // Only type is required. |
| 26 | + $plugin->setContextValue('type', 'node'); |
| 27 | + $context_values = $plugin->getContextValuesWithDefaults(); |
| 28 | + $this->assertTrue($context_values['access']); |
| 29 | + $this->assertSame('view', $context_values['access_operation']); |
| 30 | + } |
| 31 | + |
| 32 | + /** |
| 33 | + * Test that the legacy dataproducer_populate_default_values setting works. |
| 34 | + * |
| 35 | + * @dataProvider settingsProvider |
| 36 | + */ |
| 37 | + public function testLegacyDefaultValueSetting(bool $populate_setting, string $testClass): void { |
| 38 | + $this->container->get('config.factory')->getEditable('graphql.settings') |
| 39 | + ->set('dataproducer_populate_default_values', $populate_setting) |
| 40 | + ->save(); |
| 41 | + $manager = $this->container->get('plugin.manager.graphql.data_producer'); |
| 42 | + |
| 43 | + // Manipulate the plugin definitions to use our test class for entity_load. |
| 44 | + $definitions = $manager->getDefinitions(); |
| 45 | + $definitions['entity_load']['class'] = $testClass; |
| 46 | + $reflection = new \ReflectionClass($manager); |
| 47 | + $property = $reflection->getProperty('definitions'); |
| 48 | + $property->setAccessible(TRUE); |
| 49 | + $property->setValue($manager, $definitions); |
| 50 | + |
| 51 | + $this->executeDataProducer('entity_load', ['type' => 'node']); |
| 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 | + |
| 64 | +} |
| 65 | + |
| 66 | +/** |
| 67 | + * Helper class to test the legacy behavior. |
| 68 | + */ |
| 69 | +class TestLegacyEntityLoad extends EntityLoad { |
| 70 | + |
| 71 | + /** |
| 72 | + * {@inheritdoc} |
| 73 | + */ |
| 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. |
| 76 | + Assert::assertNull($access); |
| 77 | + Assert::assertNull($accessOperation); |
| 78 | + return NULL; |
| 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 | + |
| 98 | +} |
0 commit comments