Move all files to code/

This commit is contained in:
Oliver Davies 2025-10-02 08:13:02 +01:00
parent 0a1da40788
commit 8447b33ec8
27 changed files with 0 additions and 0 deletions

View file

@ -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!');
}
}

View file

@ -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);
}
}

View file

@ -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']);
}
}

View file

@ -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);
}
}