Initial commit

This commit is contained in:
Oliver Davies 2020-03-13 11:17:16 +00:00
commit e1517ecd9d
36 changed files with 2093 additions and 0 deletions
step4-adding-the-blog-repository/tests/src

View file

@ -0,0 +1,27 @@
<?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>');
}
}

View file

@ -0,0 +1,19 @@
<?php
namespace Drupal\Tests\my_module\Functional;
use Drupal\Tests\BrowserTestBase;
use Symfony\Component\HttpFoundation\Response;
class MyModuleTest extends BrowserTestBase {
protected $defaultTheme = 'stark';
/** @test */
public function the_front_page_loads_for_anonymous_users() {
$this->drupalGet('<front>');
$this->assertResponse(Response::HTTP_OK);
}
}

View file

@ -0,0 +1,75 @@
<?php
namespace Drupal\Tests\my_module\Kernel;
use Drupal\Core\Datetime\DrupalDateTime;
use Drupal\KernelTests\Core\Entity\EntityKernelTestBase;
use Drupal\my_module\Repository\ArticleRepository;
use Drupal\node\Entity\Node;
use Drupal\Tests\node\Traits\NodeCreationTrait;
class ArticleRepositoryTest extends EntityKernelTestBase {
use NodeCreationTrait;
public static $modules = [
'node',
'my_module',
];
protected function setUp() {
parent::setUp();
$this->installSchema('node', ['node_access']);
$this->installConfig([
'filter',
]);
}
/** @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();
$this->assertCount(5, Node::loadMultiple());
$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' => Node::PUBLISHED])->save();
$this->createNode(['type' => 'article', 'status' => Node::NOT_PUBLISHED])->save();
$this->createNode(['type' => 'article', 'status' => Node::PUBLISHED])->save();
$this->createNode(['type' => 'article', 'status' => Node::NOT_PUBLISHED])->save();
$this->createNode(['type' => 'article', 'status' => Node::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);
}
}