5c: Add test for isPublishable

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.
This commit is contained in:
Oliver Davies 2020-03-19 22:03:42 +00:00
parent b98d181a58
commit f0f93912ee

View file

@ -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],
];
}
}