Move all files to code/
This commit is contained in:
parent
0a1da40788
commit
8447b33ec8
27 changed files with 0 additions and 0 deletions
5
code/web/modules/custom/my_module/my_module.info.yml
Normal file
5
code/web/modules/custom/my_module/my_module.info.yml
Normal file
|
@ -0,0 +1,5 @@
|
|||
name: My Module
|
||||
type: module
|
||||
core: 8.x
|
||||
core_version_requirement: ^8 || ^9
|
||||
package: Custom
|
7
code/web/modules/custom/my_module/my_module.routing.yml
Normal file
7
code/web/modules/custom/my_module/my_module.routing.yml
Normal file
|
@ -0,0 +1,7 @@
|
|||
blog.page:
|
||||
path: /blog
|
||||
defaults:
|
||||
_controller: Drupal\my_module\Controller\BlogPageController
|
||||
_title: Blog
|
||||
requirements:
|
||||
_permission: access content
|
3
code/web/modules/custom/my_module/my_module.services.yml
Normal file
3
code/web/modules/custom/my_module/my_module.services.yml
Normal file
|
@ -0,0 +1,3 @@
|
|||
services:
|
||||
Drupal\my_module\Repository\ArticleRepository:
|
||||
autowire: true
|
|
@ -0,0 +1,18 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\my_module\Controller;
|
||||
|
||||
use Drupal\Core\StringTranslation\StringTranslationTrait;
|
||||
|
||||
class BlogPageController {
|
||||
|
||||
use StringTranslationTrait;
|
||||
|
||||
public function __invoke(): array {
|
||||
return [
|
||||
'#markup' => $this->t('Welcome to my blog!'),
|
||||
];
|
||||
}
|
||||
|
||||
}
|
||||
|
23
code/web/modules/custom/my_module/src/Entity/Post.php
Normal file
23
code/web/modules/custom/my_module/src/Entity/Post.php
Normal file
|
@ -0,0 +1,23 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\my_module\Entity;
|
||||
|
||||
use Drupal\node\NodeInterface;
|
||||
|
||||
class Post {
|
||||
|
||||
private $node;
|
||||
|
||||
public function __construct(NodeInterface $node) {
|
||||
if ($node->bundle() != 'article') {
|
||||
throw new \InvalidArgumentException();
|
||||
}
|
||||
|
||||
$this->node = $node;
|
||||
}
|
||||
|
||||
public function getTitle(): string {
|
||||
return $this->node->label();
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,36 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\my_module\Repository;
|
||||
|
||||
use Drupal\Core\Entity\EntityTypeManagerInterface;
|
||||
use Drupal\my_module\Entity\Post;
|
||||
use Drupal\node\NodeInterface;
|
||||
|
||||
class ArticleRepository {
|
||||
|
||||
private $nodeStorage;
|
||||
|
||||
public function __construct(EntityTypeManagerInterface $entityTypeManager) {
|
||||
$this->nodeStorage = $entityTypeManager->getStorage('node');
|
||||
}
|
||||
|
||||
public function getAll(): array {
|
||||
$articles = $this->nodeStorage->loadByProperties([
|
||||
'status' => NodeInterface::PUBLISHED,
|
||||
'type' => 'article',
|
||||
]);
|
||||
|
||||
$this->sortByCreatedDate($articles);
|
||||
|
||||
return array_map(function (NodeInterface $node): Post {
|
||||
return new Post($node);
|
||||
}, $articles);
|
||||
}
|
||||
|
||||
private function sortByCreatedDate(array &$articles): void {
|
||||
uasort($articles, function (NodeInterface $a, NodeInterface $b): bool {
|
||||
return $a->getCreatedTime() < $b->getCreatedTime();
|
||||
});
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,28 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\my_module\Functional;
|
||||
|
||||
use Drupal\Tests\BrowserTestBase;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
|
||||
class BlogPageTest extends BrowserTestBase {
|
||||
|
||||
protected $defaultTheme = 'stark';
|
||||
|
||||
protected static $modules = [
|
||||
'node',
|
||||
'my_module',
|
||||
];
|
||||
|
||||
/** @test */
|
||||
public function the_blog_page_loads_for_anonymous_users_and_contains_the_right_text() {
|
||||
$this->drupalGet('blog');
|
||||
|
||||
$session = $this->assertSession();
|
||||
|
||||
$session->statusCodeEquals(Response::HTTP_OK);
|
||||
$session->responseContains('<h1>Blog</h1>');
|
||||
$session->pageTextContains('Welcome to my blog!');
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,41 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\Tests\my_module\Functional;
|
||||
|
||||
use Drupal\Tests\BrowserTestBase;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
|
||||
class MyModuleTest extends BrowserTestBase {
|
||||
|
||||
protected $defaultTheme = 'stark';
|
||||
|
||||
protected static $modules = ['my_module'];
|
||||
|
||||
/** @test */
|
||||
public function the_front_page_loads_for_anonymous_users() {
|
||||
$this->drupalGet('<front>');
|
||||
|
||||
$this->assertResponse(Response::HTTP_OK);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function the_admin_page_is_not_accessible_to_anonymous_users() {
|
||||
$this->drupalGet('admin');
|
||||
|
||||
$this->assertResponse(Response::HTTP_FORBIDDEN);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function the_admin_page_is_accessible_by_admin_users() {
|
||||
$adminUser = $this->createUser([
|
||||
'access administration pages',
|
||||
]);
|
||||
|
||||
$this->drupalLogin($adminUser);
|
||||
|
||||
$this->drupalGet('admin');
|
||||
|
||||
$this->assertResponse(Response::HTTP_OK);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,82 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\Tests\my_module\Kernel;
|
||||
|
||||
use Drupal\Core\Datetime\DrupalDateTime;
|
||||
use Drupal\KernelTests\Core\Entity\EntityKernelTestBase;
|
||||
use Drupal\my_module\Entity\Post;
|
||||
use Drupal\my_module\Repository\ArticleRepository;
|
||||
use Drupal\node\NodeInterface;
|
||||
use Drupal\Tests\node\Traits\NodeCreationTrait;
|
||||
|
||||
class ArticleRepositoryTest extends EntityKernelTestBase {
|
||||
|
||||
use NodeCreationTrait;
|
||||
|
||||
public static $modules = ['node', 'my_module'];
|
||||
|
||||
/** @test */
|
||||
public function it_returns_blog_posts() {
|
||||
$this->createNode(['type' => 'article', 'title' => 'Test post'])->save();
|
||||
|
||||
$repository = $this->container->get(ArticleRepository::class);
|
||||
$articles = $repository->getAll();
|
||||
|
||||
$this->assertCount(1, $articles);
|
||||
$this->assertIsObject($articles[1]);
|
||||
$this->assertInstanceOf(Post::class, $articles[1]);
|
||||
$this->assertSame('Test post', $articles[1]->getTitle());
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function nodes_that_are_not_articles_are_not_returned() {
|
||||
$this->createNode(['type' => 'article'])->save();
|
||||
$this->createNode(['type' => 'page'])->save();
|
||||
$this->createNode(['type' => 'article'])->save();
|
||||
$this->createNode(['type' => 'page'])->save();
|
||||
$this->createNode(['type' => 'article'])->save();
|
||||
|
||||
$repository = $this->container->get(ArticleRepository::class);
|
||||
$articles = $repository->getAll();
|
||||
|
||||
$this->assertCount(3, $articles);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function only_published_articles_are_returned() {
|
||||
$this->createNode(['type' => 'article', 'status' => NodeInterface::PUBLISHED])->save();
|
||||
$this->createNode(['type' => 'article', 'status' => NodeInterface::NOT_PUBLISHED])->save();
|
||||
$this->createNode(['type' => 'article', 'status' => NodeInterface::PUBLISHED])->save();
|
||||
$this->createNode(['type' => 'article', 'status' => NodeInterface::NOT_PUBLISHED])->save();
|
||||
$this->createNode(['type' => 'article', 'status' => NodeInterface::PUBLISHED])->save();
|
||||
|
||||
$repository = $this->container->get(ArticleRepository::class);
|
||||
$articles = $repository->getAll();
|
||||
|
||||
$this->assertCount(3, $articles);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function nodes_are_ordered_by_date_and_returned_newest_first() {
|
||||
$this->createNode(['type' => 'article', 'created' => (new DrupalDateTime('-2 days'))->getTimestamp()]);
|
||||
$this->createNode(['type' => 'article', 'created' => (new DrupalDateTime('-1 week'))->getTimestamp()]);
|
||||
$this->createNode(['type' => 'article', 'created' => (new DrupalDateTime('-1 hour'))->getTimestamp()]);
|
||||
$this->createNode(['type' => 'article', 'created' => (new DrupalDateTime('-1 year'))->getTimestamp()]);
|
||||
$this->createNode(['type' => 'article', 'created' => (new DrupalDateTime('-1 month'))->getTimestamp()]);
|
||||
|
||||
$repository = $this->container->get(ArticleRepository::class);
|
||||
$nodes = $repository->getAll();
|
||||
$nodeIds = array_keys($nodes);
|
||||
|
||||
$this->assertSame([3, 1, 2, 5, 4], $nodeIds);
|
||||
}
|
||||
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
$this->installConfig(['filter']);
|
||||
|
||||
$this->installSchema('node', ['node_access']);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,36 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\Tests\my_module\Unit\Entity;
|
||||
|
||||
use Drupal\my_module\Entity\Post;
|
||||
use Drupal\node\NodeInterface;
|
||||
use Drupal\Tests\UnitTestCase;
|
||||
|
||||
class PostTest extends UnitTestCase {
|
||||
|
||||
/** @test */
|
||||
public function it_returns_the_title() {
|
||||
$node = $this->createMock(NodeInterface::class);
|
||||
|
||||
$node->expects($this->once())
|
||||
->method('label')
|
||||
->willReturn('Test post');
|
||||
$node->method('bundle')->willReturn('article');
|
||||
|
||||
$post = new Post($node);
|
||||
|
||||
$this->assertSame('Test post', $post->getTitle());
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_throws_an_exception_if_the_node_is_not_an_article() {
|
||||
$node = $this->createMock(NodeInterface::class);
|
||||
|
||||
$node->method('bundle')->willReturn('page');
|
||||
|
||||
$this->expectException(\InvalidArgumentException::class);
|
||||
|
||||
new Post($node);
|
||||
}
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue