diff --git a/web/modules/custom/my_module/tests/src/Kernel/ArticleRepositoryTest.php b/web/modules/custom/my_module/tests/src/Kernel/ArticleRepositoryTest.php index 2d3468b..f3af15e 100644 --- a/web/modules/custom/my_module/tests/src/Kernel/ArticleRepositoryTest.php +++ b/web/modules/custom/my_module/tests/src/Kernel/ArticleRepositoryTest.php @@ -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; @@ -55,6 +56,21 @@ 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); + } + protected function setUp() { parent::setUp();