@amazeelabs/silverback-gatsby 3.7.13

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (101) hide show
  1. package/CHANGELOG.md +11 -0
  2. package/drupal/silverback_gatsby/.prettierignore +1 -0
  3. package/drupal/silverback_gatsby/CHANGELOG.md +1216 -0
  4. package/drupal/silverback_gatsby/README.md +297 -0
  5. package/drupal/silverback_gatsby/composer.json +15 -0
  6. package/drupal/silverback_gatsby/directives.gql +63 -0
  7. package/drupal/silverback_gatsby/directives.graphql +62 -0
  8. package/drupal/silverback_gatsby/drush.services.yml +9 -0
  9. package/drupal/silverback_gatsby/graphql/entity.directive.graphqls +2 -0
  10. package/drupal/silverback_gatsby/graphql/menu.directive.graphqls +17 -0
  11. package/drupal/silverback_gatsby/graphql/silverback_gatsby.base.graphqls +25 -0
  12. package/drupal/silverback_gatsby/graphql/silverback_gatsby.extension.graphqls +10 -0
  13. package/drupal/silverback_gatsby/graphql/stringTranslation.directive.graphqls +4 -0
  14. package/drupal/silverback_gatsby/graphql/translatableString.directive.graphqls +2 -0
  15. package/drupal/silverback_gatsby/modules/silverback_gatsby_example/directives.gql +6 -0
  16. package/drupal/silverback_gatsby/modules/silverback_gatsby_example/graphql/.graphqlrc.json +4 -0
  17. package/drupal/silverback_gatsby/modules/silverback_gatsby_example/graphql/silverback_gatsby_example.graphqls +39 -0
  18. package/drupal/silverback_gatsby/modules/silverback_gatsby_example/silverback_gatsby_example.info.yml +7 -0
  19. package/drupal/silverback_gatsby/modules/silverback_gatsby_example/silverback_gatsby_example.module +16 -0
  20. package/drupal/silverback_gatsby/modules/silverback_gatsby_example/src/Directives.php +24 -0
  21. package/drupal/silverback_gatsby/silverback_gatsby.info.yml +8 -0
  22. package/drupal/silverback_gatsby/silverback_gatsby.install +180 -0
  23. package/drupal/silverback_gatsby/silverback_gatsby.links.task.yml +6 -0
  24. package/drupal/silverback_gatsby/silverback_gatsby.module +119 -0
  25. package/drupal/silverback_gatsby/silverback_gatsby.permissions.yml +8 -0
  26. package/drupal/silverback_gatsby/silverback_gatsby.post_update.php +15 -0
  27. package/drupal/silverback_gatsby/silverback_gatsby.routing.yml +28 -0
  28. package/drupal/silverback_gatsby/silverback_gatsby.services.yml +50 -0
  29. package/drupal/silverback_gatsby/src/Annotation/GatsbyFeed.php +41 -0
  30. package/drupal/silverback_gatsby/src/Commands/SilverbackGatsbyCommands.php +102 -0
  31. package/drupal/silverback_gatsby/src/Controller/BuildController.php +45 -0
  32. package/drupal/silverback_gatsby/src/Controller/PublisherController.php +34 -0
  33. package/drupal/silverback_gatsby/src/Directives.php +51 -0
  34. package/drupal/silverback_gatsby/src/GatsbyBuildTrigger.php +230 -0
  35. package/drupal/silverback_gatsby/src/GatsbyBuildTriggerInterface.php +52 -0
  36. package/drupal/silverback_gatsby/src/GatsbyUpdate.php +38 -0
  37. package/drupal/silverback_gatsby/src/GatsbyUpdateHandler.php +181 -0
  38. package/drupal/silverback_gatsby/src/GatsbyUpdateTracker.php +98 -0
  39. package/drupal/silverback_gatsby/src/GatsbyUpdateTrackerInterface.php +59 -0
  40. package/drupal/silverback_gatsby/src/GatsbyUpdateTrigger.php +69 -0
  41. package/drupal/silverback_gatsby/src/GatsbyUpdateTriggerInterface.php +22 -0
  42. package/drupal/silverback_gatsby/src/GraphQL/Build.php +235 -0
  43. package/drupal/silverback_gatsby/src/GraphQL/ComposableSchema.php +73 -0
  44. package/drupal/silverback_gatsby/src/LocaleStorageDecorator.php +181 -0
  45. package/drupal/silverback_gatsby/src/MenuTreeStorageDecorator.php +139 -0
  46. package/drupal/silverback_gatsby/src/Plugin/FeedBase.php +121 -0
  47. package/drupal/silverback_gatsby/src/Plugin/FeedInterface.php +132 -0
  48. package/drupal/silverback_gatsby/src/Plugin/FeedPluginManager.php +39 -0
  49. package/drupal/silverback_gatsby/src/Plugin/Gatsby/Feed/EntityFeed.php +252 -0
  50. package/drupal/silverback_gatsby/src/Plugin/Gatsby/Feed/MenuFeed.php +292 -0
  51. package/drupal/silverback_gatsby/src/Plugin/Gatsby/Feed/StringTranslationFeed.php +171 -0
  52. package/drupal/silverback_gatsby/src/Plugin/Gatsby/Feed/TranslatableStringFeed.php +128 -0
  53. package/drupal/silverback_gatsby/src/Plugin/GraphQL/DataProducer/CurrentUserEntity.php +43 -0
  54. package/drupal/silverback_gatsby/src/Plugin/GraphQL/DataProducer/EntityQueryBase.php +40 -0
  55. package/drupal/silverback_gatsby/src/Plugin/GraphQL/DataProducer/FetchEntity.php +371 -0
  56. package/drupal/silverback_gatsby/src/Plugin/GraphQL/DataProducer/FetchString.php +64 -0
  57. package/drupal/silverback_gatsby/src/Plugin/GraphQL/DataProducer/FetchTranslatableString.php +74 -0
  58. package/drupal/silverback_gatsby/src/Plugin/GraphQL/DataProducer/FocalPoint.php +47 -0
  59. package/drupal/silverback_gatsby/src/Plugin/GraphQL/DataProducer/GatsbyBuildId.php +36 -0
  60. package/drupal/silverback_gatsby/src/Plugin/GraphQL/DataProducer/GatsbyExtractId.php +28 -0
  61. package/drupal/silverback_gatsby/src/Plugin/GraphQL/DataProducer/GatsbyExtractLangcode.php +28 -0
  62. package/drupal/silverback_gatsby/src/Plugin/GraphQL/DataProducer/ImageProps.php +149 -0
  63. package/drupal/silverback_gatsby/src/Plugin/GraphQL/DataProducer/ListEntities.php +88 -0
  64. package/drupal/silverback_gatsby/src/Plugin/GraphQL/DataProducer/ListStrings.php +87 -0
  65. package/drupal/silverback_gatsby/src/Plugin/GraphQL/DataProducer/StringId.php +37 -0
  66. package/drupal/silverback_gatsby/src/Plugin/GraphQL/DataProducer/StringTranslations.php +94 -0
  67. package/drupal/silverback_gatsby/src/Plugin/GraphQL/Directive/EntityFetch.php +51 -0
  68. package/drupal/silverback_gatsby/src/Plugin/GraphQL/Directive/EntityTranslationsWithDefault.php +30 -0
  69. package/drupal/silverback_gatsby/src/Plugin/GraphQL/Directive/FocalPoint.php +27 -0
  70. package/drupal/silverback_gatsby/src/Plugin/GraphQL/Directive/ImageProps.php +27 -0
  71. package/drupal/silverback_gatsby/src/Plugin/GraphQL/Directive/MenuLangcode.php +27 -0
  72. package/drupal/silverback_gatsby/src/Plugin/GraphQL/Directive/MenuTranslations.php +36 -0
  73. package/drupal/silverback_gatsby/src/Plugin/GraphQL/Directive/SilverbackGatsbyEntityId.php +39 -0
  74. package/drupal/silverback_gatsby/src/Plugin/GraphQL/SchemaExtension/SilverbackGatsbySchemaExtension.php +384 -0
  75. package/drupal/silverback_gatsby/src/SilverbackGatsbyServiceProvider.php +43 -0
  76. package/drupal/silverback_gatsby/src/SilverbackGatsbySessionConfiguration.php +30 -0
  77. package/drupal/silverback_gatsby/src/SilverbackReverseProxyMiddleware.php +49 -0
  78. package/drupal/silverback_gatsby/tests/queries/create-page-fields.gql +12 -0
  79. package/drupal/silverback_gatsby/tests/queries/current-user.gql +6 -0
  80. package/drupal/silverback_gatsby/tests/queries/feed_info.gql +12 -0
  81. package/drupal/silverback_gatsby/tests/queries/load-entity.gql +5 -0
  82. package/drupal/silverback_gatsby/tests/queries/menus.gql +31 -0
  83. package/drupal/silverback_gatsby/tests/queries/multilingual-menus.gql +11 -0
  84. package/drupal/silverback_gatsby/tests/queries/revisionable-translatable.gql +15 -0
  85. package/drupal/silverback_gatsby/tests/queries/revisionable.gql +8 -0
  86. package/drupal/silverback_gatsby/tests/queries/translatable.gql +14 -0
  87. package/drupal/silverback_gatsby/tests/queries/untranslatable.gql +10 -0
  88. package/drupal/silverback_gatsby/tests/schema/.graphqlrc.json +4 -0
  89. package/drupal/silverback_gatsby/tests/schema/translatable-strings.graphql +7 -0
  90. package/drupal/silverback_gatsby/tests/src/Kernel/CurrentUserTest.php +37 -0
  91. package/drupal/silverback_gatsby/tests/src/Kernel/EntityFeedTest.php +490 -0
  92. package/drupal/silverback_gatsby/tests/src/Kernel/EntityFeedTestBase.php +178 -0
  93. package/drupal/silverback_gatsby/tests/src/Kernel/GatsbyBuildTriggerTest.php +155 -0
  94. package/drupal/silverback_gatsby/tests/src/Kernel/GatsbyFeedInfoTest.php +319 -0
  95. package/drupal/silverback_gatsby/tests/src/Kernel/GatsbyUpdateHandlerTest.php +131 -0
  96. package/drupal/silverback_gatsby/tests/src/Kernel/GatsbyUpdateTrackerTest.php +196 -0
  97. package/drupal/silverback_gatsby/tests/src/Kernel/GatsbyUpdateTriggerTest.php +177 -0
  98. package/drupal/silverback_gatsby/tests/src/Kernel/MenuFeedTest.php +192 -0
  99. package/drupal/silverback_gatsby/tests/src/Kernel/TranslatableStringFeedTest.php +210 -0
  100. package/drupal/silverback_gatsby/tests/src/Traits/NotificationCheckTrait.php +43 -0
  101. package/package.json +15 -0
@@ -0,0 +1,196 @@
1
+ <?php
2
+
3
+ namespace Drupal\Tests\silverback_gatsby\Kernel;
4
+
5
+ use Drupal\graphql\Entity\Server;
6
+ use Drupal\KernelTests\KernelTestBase;
7
+ use Drupal\silverback_gatsby\GatsbyUpdate;
8
+ use Drupal\Tests\silverback_gatsby\Traits\NotificationCheckTrait;
9
+
10
+ /**
11
+ *
12
+ */
13
+ class GatsbyUpdateTrackerTest extends KernelTestBase {
14
+ use NotificationCheckTrait;
15
+
16
+ protected $strictConfigSchema = FALSE;
17
+
18
+ /**
19
+ * @var string[]
20
+ */
21
+ protected static $modules = [
22
+ 'language',
23
+ 'user',
24
+ 'node',
25
+ 'graphql',
26
+ 'file',
27
+ 'content_translation',
28
+ 'graphql_directives',
29
+ 'silverback_gatsby',
30
+ 'silverback_gatsby_example',
31
+ 'menu_link_content',
32
+ 'path_alias',
33
+ ];
34
+
35
+ /**
36
+ * @var \Drupal\silverback_gatsby\GatsbyUpdateTracker|object|null
37
+ */
38
+ protected $tracker;
39
+
40
+ /**
41
+ * {@inheritdoc}
42
+ */
43
+ protected function setUp() : void {
44
+ parent::setUp();
45
+ $this->setupClientProphecy();
46
+ $this->installConfig('graphql');
47
+ $this->installConfig('graphql_directives');
48
+ $this->installSchema('silverback_gatsby', ['gatsby_update_log']);
49
+
50
+ $schemaDefinition = \Drupal::moduleHandler()->getModule('silverback_gatsby')->getPath()
51
+ . '/modules/silverback_gatsby_example/graphql/silverback_gatsby_example.graphqls';
52
+
53
+ Server::create([
54
+ 'schema' => 'directable',
55
+ 'name' => 'foo',
56
+ 'endpoint' => '/foo',
57
+ 'schema_configuration' => [
58
+ 'directable' => [
59
+ 'extensions' => [
60
+ 'silverback_gatsby' => 'silverback_gatsby',
61
+ ],
62
+ 'schema_definition' => $schemaDefinition,
63
+ 'autoload_contexts' => 'drupal',
64
+ 'build_webhook' => 'http://localhost:8000/__refresh',
65
+ ],
66
+ ],
67
+ ])->save();
68
+ Server::create([
69
+ 'schema' => 'directable',
70
+ 'name' => 'bar',
71
+ 'endpoint' => '/bar',
72
+ 'schema_configuration' => [
73
+ 'directable' => [
74
+ 'extensions' => [
75
+ 'silverback_gatsby' => 'silverback_gatsby',
76
+ ],
77
+ 'schema_definition' => $schemaDefinition,
78
+ 'autoload_contexts' => 'drupal',
79
+ 'build_webhook' => 'http://localhost:8000/__refresh',
80
+ ],
81
+ ],
82
+ ])->save();
83
+ $this->tracker = $this->container->get('silverback_gatsby.update_tracker');
84
+ }
85
+
86
+ /**
87
+ *
88
+ */
89
+ public function testNoBuilds() {
90
+ $this->assertEquals(-1, $this->tracker->latestBuild('foo'));
91
+ $this->assertEmpty($this->tracker->diff(1, 2, 'foo'));
92
+ _drupal_shutdown_function();
93
+ $this->checkTotalNotifications(0);
94
+ }
95
+
96
+ /**
97
+ *
98
+ */
99
+ public function testSingleBuild() {
100
+ $this->tracker->track('foo', 'Page', '1');
101
+ $this->assertEquals(1, $this->tracker->latestBuild('foo'));
102
+ _drupal_shutdown_function();
103
+ $this->checkTotalNotifications(1);
104
+ }
105
+
106
+ /**
107
+ *
108
+ */
109
+ public function testMultipleBuilds() {
110
+ $this->tracker->track('foo', 'Page', '1');
111
+ $this->tracker->track('foo', 'Page', '2');
112
+ $this->assertEquals(2, $this->tracker->latestBuild('foo'));
113
+ $this->assertEquals([new GatsbyUpdate('Page', '2')],
114
+ $this->tracker->diff(1, 2, 'foo')
115
+ );
116
+ }
117
+
118
+ /**
119
+ *
120
+ */
121
+ public function testDeferredBuild() {
122
+ $this->tracker->track('foo', 'Page', '1', FALSE);
123
+ $this->assertEquals(1, $this->tracker->latestBuild('foo'));
124
+ _drupal_shutdown_function();
125
+ $this->checkTotalNotifications(0);
126
+ }
127
+
128
+ /**
129
+ *
130
+ */
131
+ public function testInvalidDiff() {
132
+ $this->tracker->track('foo', 'Page', '1');
133
+ $this->tracker->track('foo', 'Page', '2');
134
+
135
+ // If one of the builds does not exist, it returns an empty list.
136
+ // We are not sure about the history then, so we have to run a full rebuild.
137
+ $this->assertEquals([],
138
+ $this->tracker->diff(0, 2, 'foo')
139
+ );
140
+
141
+ $this->assertEquals([],
142
+ $this->tracker->diff(1, 3, 'foo')
143
+ );
144
+
145
+ $this->assertEquals([],
146
+ $this->tracker->diff(0, 4, 'foo')
147
+ );
148
+
149
+ // In reverse order it will return an empty list too.
150
+ $this->assertEquals([],
151
+ $this->tracker->diff(2, 1, 'foo')
152
+ );
153
+ }
154
+
155
+ /**
156
+ *
157
+ */
158
+ public function testMultipleServers() {
159
+ $this->tracker->track('foo', 'Page', '1');
160
+ $this->tracker->track('foo', 'Page', '2');
161
+ $this->tracker->track('bar', 'Page', '1');
162
+ $this->tracker->track('bar', 'Page', '3');
163
+
164
+ $this->assertEquals(2, $this->tracker->latestBuild('foo'));
165
+ $this->assertEquals(4, $this->tracker->latestBuild('bar'));
166
+
167
+ $this->assertEquals([
168
+ new GatsbyUpdate('Page', '2'),
169
+ ], $this->tracker->diff(1, 2, 'foo'));
170
+
171
+ $this->assertEquals([], $this->tracker->diff(1, 2, 'bar'));
172
+
173
+ $this->assertEquals([
174
+ new GatsbyUpdate('Page', '3'),
175
+ ], $this->tracker->diff(3, 4, 'bar'));
176
+ }
177
+
178
+ public function testDuplicateItems() {
179
+ $this->tracker->track('foo', 'Page', '1');
180
+ $this->tracker->track('foo', 'Page', '2');
181
+
182
+ // Simulate a new PHP request to track a duplicate. Otherwise, it won't be
183
+ // tracked.
184
+ $this->tracker->clear();
185
+
186
+ // Add a duplicate entry.
187
+ $this->tracker->track('foo', 'Page', '2');
188
+
189
+ // Expect no duplicates in the diff.
190
+ $this->assertEquals(
191
+ [new GatsbyUpdate('Page', '2')],
192
+ $this->tracker->diff(1, 3, 'foo')
193
+ );
194
+ }
195
+
196
+ }
@@ -0,0 +1,177 @@
1
+ <?php
2
+
3
+ namespace Drupal\Tests\silverback_gatsby\Kernel;
4
+
5
+ use Drupal\Core\Messenger\MessengerInterface;
6
+ use Drupal\graphql\Entity\Server;
7
+ use Drupal\KernelTests\KernelTestBase;
8
+ use Drupal\silverback_gatsby\GatsbyUpdate;
9
+ use Drupal\Tests\silverback_gatsby\Traits\NotificationCheckTrait;
10
+ use GuzzleHttp\Exception\RequestException;
11
+ use GuzzleHttp\Psr7\Request;
12
+ use Prophecy\Argument;
13
+
14
+ /**
15
+ *
16
+ */
17
+ class GatsbyUpdateTriggerTest extends KernelTestBase {
18
+ use NotificationCheckTrait;
19
+
20
+ protected $strictConfigSchema = FALSE;
21
+
22
+ /**
23
+ * @var string[]
24
+ */
25
+ protected static $modules = [
26
+ 'language',
27
+ 'user',
28
+ 'node',
29
+ 'graphql',
30
+ 'file',
31
+ 'content_translation',
32
+ 'graphql_directives',
33
+ 'silverback_gatsby',
34
+ 'silverback_gatsby_example',
35
+ 'menu_link_content',
36
+ 'path_alias',
37
+ ];
38
+
39
+ /**
40
+ * @var \Prophecy\Prophecy\ObjectProphecy
41
+ */
42
+ protected $messengerProphecy;
43
+
44
+ /**
45
+ * @var \Drupal\silverback_gatsby\GatsbyUpdateTrigger
46
+ */
47
+ protected $trigger;
48
+
49
+ /**
50
+ * {@inheritdoc}
51
+ */
52
+ protected function setUp() : void {
53
+ parent::setUp();
54
+ $this->setupClientProphecy();
55
+ $this->messengerProphecy = $this->prophesize(MessengerInterface::class);
56
+ $this->container->set('messenger', $this->messengerProphecy->reveal());
57
+
58
+ $this->installConfig('graphql');
59
+ $this->installConfig('graphql_directives');
60
+ $this->installSchema('silverback_gatsby', ['gatsby_update_log']);
61
+
62
+ $this->trigger = $this->container->get('silverback_gatsby.update_trigger');
63
+
64
+ $schemaDefinition = \Drupal::moduleHandler()->getModule('silverback_gatsby')->getPath()
65
+ . '/modules/silverback_gatsby_example/graphql/silverback_gatsby_example.graphqls';
66
+
67
+ Server::create([
68
+ 'schema' => 'directable',
69
+ 'name' => 'foo',
70
+ 'endpoint' => '/foo',
71
+ 'schema_configuration' => [
72
+ 'directable' => [
73
+ 'extensions' => [
74
+ 'silverback_gatsby' => 'silverback_gatsby',
75
+ ],
76
+ 'schema_definition' => $schemaDefinition,
77
+ 'autoload_contexts' => 'drupal',
78
+ 'build_webhook' => 'http://localhost:8000/__refresh',
79
+ 'update_webhook' => 'http://localhost:8000/__update',
80
+ ],
81
+ ],
82
+ ])->save();
83
+
84
+ Server::create([
85
+ 'schema' => 'directable',
86
+ 'name' => 'bar',
87
+ 'endpoint' => '/bar',
88
+ 'schema_configuration' => [
89
+ 'directable' => [
90
+ 'extensions' => [
91
+ 'silverback_gatsby' => 'silverback_gatsby',
92
+ ],
93
+ 'schema_definition' => $schemaDefinition,
94
+ 'autoload_contexts' => 'drupal',
95
+ 'build_webhook' => 'http://localhost:9000/__refresh',
96
+ 'update_webhook' => 'http://localhost:9000/__update',
97
+ ],
98
+ ],
99
+ ])->save();
100
+
101
+ }
102
+
103
+ /**
104
+ *
105
+ */
106
+ public function testBeforeShutdown() {
107
+ $this->trigger->trigger('foo', new GatsbyUpdate('Page', '1'));
108
+ // If _drupal_shutdown_function() is not called, no notifications go out.
109
+ $this->checkTotalNotifications(0);
110
+ }
111
+
112
+ /**
113
+ *
114
+ */
115
+ public function testRequestException() {
116
+ $this->clientProphecy->post(Argument::any(), Argument::any())
117
+ ->willThrow(new RequestException('Invalid!', new Request('post', 'http://localhost:8000/__update')));
118
+ $this->trigger->trigger('foo', new GatsbyUpdate('Page', '1'));
119
+ _drupal_shutdown_function();
120
+ $this->messengerProphecy->addError(Argument::any())->shouldHaveBeenCalledTimes(2);
121
+ $this->messengerProphecy->addError('Invalid!')->shouldHaveBeenCalledTimes(1);
122
+ $this->messengerProphecy->addError('Could not send build notification to server "http://localhost:8000/__update".')->shouldHaveBeenCalledTimes(1);
123
+ }
124
+
125
+ /**
126
+ *
127
+ */
128
+ public function testSingleTrigger() {
129
+ $this->trigger->trigger('foo', new GatsbyUpdate("Page", "1"));
130
+ _drupal_shutdown_function();
131
+ $this->checkTotalNotifications(1);
132
+ $this->checkUpdateNotification('http://localhost:8000/__update', [[
133
+ 'type' => 'Page',
134
+ 'id' => '1',
135
+ ],
136
+ ]);
137
+ }
138
+
139
+ /**
140
+ *
141
+ */
142
+ public function testMultipleTriggers() {
143
+ $this->trigger->trigger('foo', new GatsbyUpdate("Page", "1"));
144
+ $this->trigger->trigger('foo', new GatsbyUpdate("Page", "2"));
145
+ _drupal_shutdown_function();
146
+ $this->checkTotalNotifications(1);
147
+ $this->checkUpdateNotification('http://localhost:8000/__update', [[
148
+ 'type' => 'Page',
149
+ 'id' => '1',
150
+ ], [
151
+ 'type' => 'Page',
152
+ 'id' => '2',
153
+ ],
154
+ ]);
155
+ }
156
+
157
+ /**
158
+ *
159
+ */
160
+ public function testMultipleServers() {
161
+ $this->trigger->trigger('foo', new GatsbyUpdate("Page", "1"));
162
+ $this->trigger->trigger('bar', new GatsbyUpdate("Page", "2"));
163
+ _drupal_shutdown_function();
164
+ $this->checkTotalNotifications(2);
165
+ $this->checkUpdateNotification('http://localhost:8000/__update', [[
166
+ 'type' => 'Page',
167
+ 'id' => '1',
168
+ ],
169
+ ]);
170
+ $this->checkUpdateNotification('http://localhost:9000/__update', [[
171
+ 'type' => 'Page',
172
+ 'id' => '2',
173
+ ],
174
+ ]);
175
+ }
176
+
177
+ }
@@ -0,0 +1,192 @@
1
+ <?php
2
+
3
+ namespace Drupal\Tests\silverback_gatsby\Kernel;
4
+
5
+ use Drupal\menu_link_content\Entity\MenuLinkContent;
6
+ use Drupal\menu_link_content\MenuLinkContentInterface;
7
+ use Drupal\silverback_gatsby\GatsbyUpdate;
8
+ use Drupal\system\Entity\Menu;
9
+ use Drupal\Tests\graphql\Kernel\GraphQLTestBase;
10
+
11
+ class MenuFeedTest extends GraphQLTestBase {
12
+
13
+ protected $strictConfigSchema = FALSE;
14
+ protected $itemCount;
15
+
16
+ protected static $modules = [
17
+ 'path_alias',
18
+ 'graphql_directives',
19
+ 'silverback_gatsby',
20
+ 'silverback_gutenberg',
21
+ 'silverback_gatsby_example',
22
+ ];
23
+
24
+ protected function setUp(): void {
25
+ parent::setUp();
26
+ $this->itemCount = 0;
27
+ // Silverback Gatsby setup.
28
+ $this->installConfig('graphql_directives');
29
+ $this->installSchema('silverback_gatsby', ['gatsby_update_log']);
30
+ $this->installEntitySchema('path_alias');
31
+
32
+ $schemaDefinition = \Drupal::moduleHandler()->getModule('silverback_gatsby')->getPath()
33
+ . '/modules/silverback_gatsby_example/graphql/silverback_gatsby_example.graphqls';
34
+
35
+ $this->createTestServer(
36
+ 'directable',
37
+ '/gatsby',
38
+ [
39
+ 'schema_configuration' => [
40
+ 'directable' => [
41
+ 'extensions' => [
42
+ 'silverback_gatsby' => 'silverback_gatsby'
43
+ ],
44
+ 'schema_definition' => $schemaDefinition,
45
+ 'autoload_contexts' => 'drupal',
46
+ 'build_webhook' => 'http://127.0.0.1:8888/__refresh'
47
+ ]
48
+ ]
49
+ ]
50
+ );
51
+
52
+ // Set up the menu system.
53
+ $this->installEntitySchema('menu_link_content');
54
+ // Create a menu nobody has access to (see `silverback_gatsby_example.module).
55
+ Menu::create([
56
+ 'id' => 'access_denied',
57
+ 'label' => 'Access denied',
58
+ ])->save();
59
+
60
+ $this->container->get('silverback_gatsby.update_handler')->schemaCache = NULL;
61
+ }
62
+
63
+ protected function createMenuItem(string $label, string $url, ?MenuLinkContentInterface $parent = null, $menu = 'main'): MenuLinkContentInterface {
64
+ $item = MenuLinkContent::create([
65
+ 'provider' => 'silverback_gatsby',
66
+ 'menu_name' => $menu,
67
+ 'title' => $label,
68
+ 'link' => ['uri' => $url],
69
+ 'weight' => $this->itemCount++,
70
+ ] + ($parent ? ['parent' => $parent->getPluginId()] : []));
71
+ $item->save();
72
+ return $item;
73
+ }
74
+
75
+ public function testUpdateMenuItem() {
76
+ $foo = $this->createMenuItem('Foo', 'internal:/foo');
77
+ $bar = $this->createMenuItem('Bar', 'internal:/bar', $foo);
78
+ $baz = $this->createMenuItem('Baz', 'internal:/baz', $bar);
79
+
80
+ $tracker = $this->container->get('silverback_gatsby.update_tracker');
81
+
82
+ // Change
83
+ $latest = $tracker->latestBuild($this->server->id());
84
+ $tracker->clear();
85
+ $baz->title = 'BAZ';
86
+ $baz->save();
87
+ $current = $tracker->latestBuild($this->server->id());
88
+
89
+ $diff = $tracker->diff($latest, $current, $this->server->id());
90
+ $this->assertEquals([
91
+ // This should update the 'MainMenu' type only, since the change happened
92
+ // below the visible two levels of 'VisibleMainMenu'.
93
+ new GatsbyUpdate('MainMenu', 'main'),
94
+ ], $diff);
95
+
96
+ $latest = $current;
97
+ $tracker->clear();
98
+ // Move $baz to level, which will trigger an update in both menus.
99
+ $baz->parent = $foo->getPluginId();
100
+ $baz->save();
101
+ $current = $tracker->latestBuild($this->server->id());
102
+
103
+ $diff = $tracker->diff($latest, $current, $this->server->id());
104
+ $this->assertEquals([
105
+ new GatsbyUpdate('MainMenu', 'main'),
106
+ new GatsbyUpdate('VisibleMainMenu', 'main'),
107
+ ], $diff);
108
+
109
+ $latest = $current;
110
+ $tracker->clear();
111
+ // Move $baz back down, which should also trigger an update in both menus.
112
+ $baz->parent = $bar->getPluginId();
113
+ $baz->save();
114
+ $current = $tracker->latestBuild($this->server->id());
115
+
116
+ $diff = $tracker->diff($latest, $current, $this->server->id());
117
+ $this->assertEquals([
118
+ new GatsbyUpdate('MainMenu', 'main'),
119
+ new GatsbyUpdate('VisibleMainMenu', 'main'),
120
+ ], $diff);
121
+ }
122
+
123
+ public function testUpdateMultilingualMenu() {
124
+ $this->container
125
+ ->get('content_translation.manager')
126
+ ->setEnabled('menu_link_content', 'menu_link_content', TRUE);
127
+ $translated = $this->createMenuItem('English', 'internal:/translated');
128
+
129
+ $tracker = $this->container->get('silverback_gatsby.update_tracker');
130
+ $latest = $tracker->latestBuild($this->server->id());
131
+ $tracker->clear();
132
+
133
+ $translated->addTranslation('de', [
134
+ 'title' => 'German',
135
+ ])->save();
136
+
137
+ $current = $tracker->latestBuild($this->server->id());
138
+ $tracker->clear();
139
+
140
+ $diff = $tracker->diff($latest, $current, $this->server->id());
141
+ $this->assertEquals([
142
+ // This should update the 'MainMenu' type only, since the change happened
143
+ // below the visible two levels of 'VisibleMainMenu'.
144
+ new GatsbyUpdate('MainMenu', 'main:en'),
145
+ new GatsbyUpdate('MainMenu', 'main:fr'),
146
+ new GatsbyUpdate('MainMenu', 'main:de'),
147
+ new GatsbyUpdate('VisibleMainMenu', 'main:en'),
148
+ new GatsbyUpdate('VisibleMainMenu', 'main:fr'),
149
+ new GatsbyUpdate('VisibleMainMenu', 'main:de'),
150
+ ], $diff);
151
+ }
152
+
153
+ public function testDeletedMenuItem() {
154
+ $foo = $this->createMenuItem('Foo', 'internal:/foo');
155
+
156
+ $tracker = $this->container->get('silverback_gatsby.update_tracker');
157
+
158
+ // Change
159
+ $tracker->clear();
160
+ $latest = $tracker->latestBuild($this->server->id());
161
+ $foo->delete();
162
+ $current = $tracker->latestBuild($this->server->id());
163
+
164
+ $diff = $tracker->diff($latest, $current, $this->server->id());
165
+ $this->assertEquals([
166
+ new GatsbyUpdate('MainMenu', 'main'),
167
+ new GatsbyUpdate('VisibleMainMenu', 'main'),
168
+ ], $diff);
169
+ }
170
+
171
+ public function testTranslatableMenu() {
172
+ $this->container
173
+ ->get('content_translation.manager')
174
+ ->setEnabled('menu_link_content', 'menu_link_content', TRUE);
175
+
176
+ $query = $this->getQueryFromFile('multilingual-menus.gql');
177
+ $this->assertResults($query, [], [
178
+ 'noLanguageDefined' => [
179
+ '_id' => 'main:en',
180
+ ],
181
+ 'en' => [
182
+ '_id' => 'main:en',
183
+ ],
184
+ 'de' => [
185
+ '_id' => 'main:de',
186
+ ],
187
+ ], $this->defaultCacheMetaData()
188
+ ->addCacheContexts(['languages:language_interface'])
189
+ ->addCacheTags(['config:system.menu.main'])
190
+ );
191
+ }
192
+ }