feat(blog): add article repository
This commit is contained in:
parent
09d4c662be
commit
f09464f9f7
3 changed files with 138 additions and 0 deletions
|
@ -0,0 +1,98 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Drupal\Tests\my_module\Kernel\Repository;
|
||||
|
||||
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;
|
||||
|
||||
final class ArticleRepositoryTest extends EntityKernelTestBase {
|
||||
|
||||
use NodeCreationTrait;
|
||||
|
||||
protected $strictConfigSchema = FALSE;
|
||||
|
||||
public static $modules = [
|
||||
'node',
|
||||
'my_module',
|
||||
];
|
||||
|
||||
protected function setUp(): void {
|
||||
parent::setUp();
|
||||
|
||||
$this->installSchema(module: 'node', tables: ['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);
|
||||
}
|
||||
|
||||
}
|
Reference in a new issue