From 99204d78df5d40ccf2dc2fd3d80b383851625f05 Mon Sep 17 00:00:00 2001 From: Oliver Davies Date: Mon, 8 Jan 2024 23:38:01 +0000 Subject: [PATCH] Add lesson 3 - Add failing blog page test. - Add custom routing, need to create Controller. - Add BlogPageController. - Test posts are visible. --- web/modules/custom/example/example.info.yml | 3 ++ .../custom/example/example.routing.yml | 7 ++++ .../src/Controller/BlogPageController.php | 22 +++++++++++++ .../tests/src/Functional/BlogPageTest.php | 33 +++++++++++++++++++ 4 files changed, 65 insertions(+) create mode 100644 web/modules/custom/example/example.info.yml create mode 100644 web/modules/custom/example/example.routing.yml create mode 100644 web/modules/custom/example/src/Controller/BlogPageController.php create mode 100644 web/modules/custom/example/tests/src/Functional/BlogPageTest.php diff --git a/web/modules/custom/example/example.info.yml b/web/modules/custom/example/example.info.yml new file mode 100644 index 0000000..fb0eab2 --- /dev/null +++ b/web/modules/custom/example/example.info.yml @@ -0,0 +1,3 @@ +name: Example +type: module +core_version_requirement: ^10 diff --git a/web/modules/custom/example/example.routing.yml b/web/modules/custom/example/example.routing.yml new file mode 100644 index 0000000..0077a11 --- /dev/null +++ b/web/modules/custom/example/example.routing.yml @@ -0,0 +1,7 @@ +example.blog: + path: /blog + defaults: + _controller: Drupal\example\Controller\BlogPageController + _title: Blog + requirements: + _permission: access content diff --git a/web/modules/custom/example/src/Controller/BlogPageController.php b/web/modules/custom/example/src/Controller/BlogPageController.php new file mode 100644 index 0000000..cbcd1f6 --- /dev/null +++ b/web/modules/custom/example/src/Controller/BlogPageController.php @@ -0,0 +1,22 @@ +entityTypeManager()->getStorage('node'); + $nodes = $nodeStorage->loadMultiple(); + + $build = []; + $build['content']['#theme'] = 'item_list'; + foreach ($nodes as $node) { + $build['content']['#items'][] = $node->label(); + } + + return $build; + } + +} diff --git a/web/modules/custom/example/tests/src/Functional/BlogPageTest.php b/web/modules/custom/example/tests/src/Functional/BlogPageTest.php new file mode 100644 index 0000000..c2875f3 --- /dev/null +++ b/web/modules/custom/example/tests/src/Functional/BlogPageTest.php @@ -0,0 +1,33 @@ +drupalGet('/blog'); + + $this->assertSession()->statusCodeEquals(Response::HTTP_OK); + } + + public function testPostsAreVisible(): void { + $this->createNode(['type' => 'post', 'title' => 'First post']); + $this->createNode(['type' => 'post', 'title' => 'Second post']); + $this->createNode(['type' => 'post', 'title' => 'Third post']); + + $this->drupalGet('/blog'); + + $assert = $this->assertSession(); + $assert->pageTextContains('First post'); + $assert->pageTextContains('Second post'); + $assert->pageTextContains('Third post'); + } + +}