4j: Add a test for the article ordering

Add a test to ensure that the articles are returned from the article
repository based on their created date, with the newest articles
returned first.

The articles are created with defined created dates and created in a
specific order to ensure that the test doesn’t pass by default.
This commit is contained in:
Oliver Davies 2020-03-19 21:37:38 +00:00
parent 92ca064737
commit 87fb45dcbb

View file

@ -2,6 +2,7 @@
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\NodeInterface;
@ -54,5 +55,20 @@ class ArticleRepositoryTest extends EntityKernelTestBase {
$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);
}
}