Add lesson 3
- Add failing blog page test. - Add custom routing, need to create Controller. - Add BlogPageController. - Test posts are visible.
This commit is contained in:
parent
e931656b68
commit
99204d78df
3
web/modules/custom/example/example.info.yml
Normal file
3
web/modules/custom/example/example.info.yml
Normal file
|
@ -0,0 +1,3 @@
|
|||
name: Example
|
||||
type: module
|
||||
core_version_requirement: ^10
|
7
web/modules/custom/example/example.routing.yml
Normal file
7
web/modules/custom/example/example.routing.yml
Normal file
|
@ -0,0 +1,7 @@
|
|||
example.blog:
|
||||
path: /blog
|
||||
defaults:
|
||||
_controller: Drupal\example\Controller\BlogPageController
|
||||
_title: Blog
|
||||
requirements:
|
||||
_permission: access content
|
|
@ -0,0 +1,22 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\example\Controller;
|
||||
|
||||
use Drupal\Core\Controller\ControllerBase;
|
||||
|
||||
class BlogPageController extends ControllerBase {
|
||||
|
||||
public function __invoke(): array {
|
||||
$nodeStorage = $this->entityTypeManager()->getStorage('node');
|
||||
$nodes = $nodeStorage->loadMultiple();
|
||||
|
||||
$build = [];
|
||||
$build['content']['#theme'] = 'item_list';
|
||||
foreach ($nodes as $node) {
|
||||
$build['content']['#items'][] = $node->label();
|
||||
}
|
||||
|
||||
return $build;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,33 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\Tests\example\Functional;
|
||||
|
||||
use Drupal\Tests\BrowserTestBase;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
|
||||
class BlogPageTest extends BrowserTestBase {
|
||||
|
||||
protected $defaultTheme = 'stark';
|
||||
|
||||
protected static $modules = ['node', 'example'];
|
||||
|
||||
public function testBlogPage(): void {
|
||||
$this->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');
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in a new issue