From f0f93912eeb6ba4eba0b8d5d37ee5f9d7104a7a6 Mon Sep 17 00:00:00 2001 From: Oliver Davies Date: Thu, 19 Mar 2020 22:03:42 +0000 Subject: [PATCH] 5c: Add test for isPublishable MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Using a data provider, multiple arguments can be passed into the test, such as different combinations of created dates and expected results. This test also requires mocking the `Time` object as it’s now a dependency of the article wrapper. The `getCreatedTime` method on the article also needs to be set to return a specific value. --- .../src/Unit/Wrapper/ArticleWrapperTest.php | 42 ++++++++++++++++++- 1 file changed, 40 insertions(+), 2 deletions(-) diff --git a/web/modules/custom/my_module/tests/src/Unit/Wrapper/ArticleWrapperTest.php b/web/modules/custom/my_module/tests/src/Unit/Wrapper/ArticleWrapperTest.php index d1a8be8..64b2b24 100644 --- a/web/modules/custom/my_module/tests/src/Unit/Wrapper/ArticleWrapperTest.php +++ b/web/modules/custom/my_module/tests/src/Unit/Wrapper/ArticleWrapperTest.php @@ -2,6 +2,7 @@ namespace Drupal\Tests\my_module\Unit\Wrapper; +use Drupal\Component\Datetime\TimeInterface; use Drupal\my_module\Wrapper\ArticleWrapper; use Drupal\node\NodeInterface; use Drupal\Tests\UnitTestCase; @@ -14,7 +15,8 @@ class ArticleWrapperTest extends UnitTestCase { $article->method('id')->willReturn(5); $article->method('bundle')->willReturn('article'); - $articleWrapper = new ArticleWrapper($article); + $time = $this->createMock(TimeInterface::class); + $articleWrapper = new ArticleWrapper($time, $article); $this->assertInstanceOf(NodeInterface::class, $articleWrapper->getOriginal()); $this->assertSame(5, $articleWrapper->getOriginal()->id()); @@ -28,7 +30,43 @@ class ArticleWrapperTest extends UnitTestCase { $page = $this->createMock(NodeInterface::class); $page->method('bundle')->willReturn('page'); - new ArticleWrapper($page); + $time = $this->createMock(TimeInterface::class); + new ArticleWrapper($time, $page); + } + + /** + * @test + * @dataProvider articleCreatedDateProvider + */ + public function articles_created_less_than_3_days_ago_are_not_publishable( + string $offset, + bool $expected + ) { + $time = $this->createMock(TimeInterface::class); + + $time->method('getRequestTime')->willReturn( + (new \DateTime())->getTimestamp() + ); + + $article = $this->createMock(NodeInterface::class); + $article->method('bundle')->willReturn('article'); + + $article->method('getCreatedTime')->willReturn( + (new \DateTime())->modify($offset)->getTimestamp() + ); + + $articleWrapper = new ArticleWrapper($time, $article); + + $this->assertSame($expected, $articleWrapper->isPublishable()); + } + + public function articleCreatedDateProvider() { + return [ + ['-1 day', FALSE], + ['-2 days 59 minutes', FALSE], + ['-3 days', TRUE], + ['-1 week', TRUE], + ]; } }