44
55namespace Drupal \Tests \jsonapi_frontend_layout \Kernel ;
66
7+ use Drupal \block_content \Entity \BlockContent ;
8+ use Drupal \block_content \Entity \BlockContentType ;
79use Drupal \KernelTests \KernelTestBase ;
810use Drupal \layout_builder \Section ;
911use Drupal \layout_builder \SectionComponent ;
@@ -66,7 +68,18 @@ protected function setUp(): void {
6668 'name ' => 'Page ' ,
6769 ])->save ();
6870
69- $ this ->enableLayoutBuilderOnPageDisplay ();
71+ BlockContentType::create ([
72+ 'id ' => 'basic ' ,
73+ 'label ' => 'Basic block ' ,
74+ ])->save ();
75+
76+ $ block = BlockContent::create ([
77+ 'type ' => 'basic ' ,
78+ 'info ' => 'Test block ' ,
79+ ]);
80+ $ block ->save ();
81+
82+ $ this ->enableLayoutBuilderOnPageDisplay ((int ) $ block ->getRevisionId ());
7083 }
7184
7285 public function testLayoutResolveIncludesLayoutTree (): void {
@@ -103,9 +116,62 @@ public function testLayoutResolveIncludesLayoutTree(): void {
103116
104117 $ component_types = array_map (static fn (array $ c ) => $ c ['type ' ] ?? NULL , $ first_section ['components ' ]);
105118 $ this ->assertContains ('field ' , $ component_types );
119+ $ this ->assertContains ('inline_block ' , $ component_types );
120+ $ this ->assertContains ('block ' , $ component_types );
121+ }
122+
123+ public function testResolveReturns400WhenPathMissing (): void {
124+ $ controller = \Drupal \jsonapi_frontend_layout \Controller \LayoutResolverController::create ($ this ->container );
125+ $ request = Request::create ('/jsonapi/layout/resolve ' , 'GET ' , [
126+ '_format ' => 'json ' ,
127+ ]);
128+
129+ $ response = $ controller ->resolve ($ request );
130+ $ this ->assertSame (400 , $ response ->getStatusCode ());
131+ }
132+
133+ public function testLayoutIsOmittedWhenLayoutBuilderDisabled (): void {
134+ NodeType::create ([
135+ 'type ' => 'article ' ,
136+ 'name ' => 'Article ' ,
137+ ])->save ();
138+
139+ // Ensure a display exists, but keep Layout Builder disabled.
140+ $ display = $ this ->container ->get ('entity_type.manager ' )
141+ ->getStorage ('entity_view_display ' )
142+ ->create ([
143+ 'id ' => 'node.article.default ' ,
144+ 'targetEntityType ' => 'node ' ,
145+ 'bundle ' => 'article ' ,
146+ 'mode ' => 'default ' ,
147+ 'status ' => TRUE ,
148+ ]);
149+ $ display ->save ();
150+
151+ $ node = Node::create ([
152+ 'type ' => 'article ' ,
153+ 'title ' => 'No Layout Builder ' ,
154+ 'status ' => 1 ,
155+ 'path ' => ['alias ' => '/no-layout ' ],
156+ ]);
157+ $ node ->save ();
158+
159+ $ controller = \Drupal \jsonapi_frontend_layout \Controller \LayoutResolverController::create ($ this ->container );
160+ $ request = Request::create ('/jsonapi/layout/resolve ' , 'GET ' , [
161+ 'path ' => '/no-layout ' ,
162+ '_format ' => 'json ' ,
163+ ]);
164+
165+ $ response = $ controller ->resolve ($ request );
166+ $ payload = json_decode ((string ) $ response ->getContent (), TRUE );
167+
168+ $ this ->assertIsArray ($ payload );
169+ $ this ->assertTrue ($ payload ['resolved ' ]);
170+ $ this ->assertSame ($ node ->uuid (), $ payload ['entity ' ]['id ' ]);
171+ $ this ->assertArrayNotHasKey ('layout ' , $ payload );
106172 }
107173
108- private function enableLayoutBuilderOnPageDisplay (): void {
174+ private function enableLayoutBuilderOnPageDisplay (int $ block_revision_id ): void {
109175 /** @var \Drupal\layout_builder\Entity\LayoutBuilderEntityViewDisplay $display */
110176 $ display = $ this ->container ->get ('entity_type.manager ' )
111177 ->getStorage ('entity_view_display ' )
@@ -131,6 +197,22 @@ private function enableLayoutBuilderOnPageDisplay(): void {
131197 ]);
132198 $ section ->appendComponent ($ component );
133199
200+ $ inline_block = new SectionComponent ('component-2 ' , 'content ' , [
201+ 'id ' => 'inline_block:basic ' ,
202+ 'label ' => 'Inline block ' ,
203+ 'label_display ' => FALSE ,
204+ 'block_revision_id ' => $ block_revision_id ,
205+ 'view_mode ' => 'full ' ,
206+ ]);
207+ $ section ->appendComponent ($ inline_block );
208+
209+ $ unknown_block = new SectionComponent ('component-3 ' , 'content ' , [
210+ 'id ' => 'system_powered_by_block ' ,
211+ 'label ' => 'Powered by ' ,
212+ 'label_display ' => FALSE ,
213+ ]);
214+ $ section ->appendComponent ($ unknown_block );
215+
134216 $ display ->appendSection ($ section );
135217 $ display ->save ();
136218 }
0 commit comments