NEW Add tests for blog archive links and tidy up code re-use a little

This commit is contained in:
Robbie Averill 2019-02-12 19:03:20 +07:00
parent 078c877cbb
commit dd8e401dbf
2 changed files with 42 additions and 12 deletions

View File

@ -609,18 +609,15 @@ class BlogPost extends Page
* Returns a monthly archive link for the current blog post. * Returns a monthly archive link for the current blog post.
* *
* @param string $type * @param string $type
*
* @return string * @return string
*/ */
public function getMonthlyArchiveLink($type = 'day') public function getMonthlyArchiveLink($type = 'day')
{ {
/** /** @var DBDatetime $date */
* @var DBDatetime $date
*/
$date = $this->dbObject('PublishDate'); $date = $this->dbObject('PublishDate');
if ($type != 'year') { if ($type !== 'year') {
if ($type == 'day') { if ($type === 'day') {
return Controller::join_links( return Controller::join_links(
$this->Parent()->Link('archive'), $this->Parent()->Link('archive'),
$date->format('Y'), $date->format('Y'),
@ -642,12 +639,7 @@ class BlogPost extends Page
*/ */
public function getYearlyArchiveLink() public function getYearlyArchiveLink()
{ {
/** return $this->getMonthlyArchiveLink('year');
* @var DBDatetime $date
*/
$date = $this->dbObject('PublishDate');
return Controller::join_links($this->Parent()->Link('archive'), $date->format('Y'));
} }
/** /**

View File

@ -146,4 +146,42 @@ class BlogPostTest extends SapphireTest
$this->expectException(\InvalidArgumentException::class); $this->expectException(\InvalidArgumentException::class);
$blogPost->MinutesToRead('not-a-number'); $blogPost->MinutesToRead('not-a-number');
} }
/**
* @param string $type
* @param string $expected
* @dataProvider monthlyArchiveLinkProvider
* @group wip
*/
public function testGetMonthlyArchiveLink($type, $expected)
{
/** @var BlogPost $blogPost */
$blogPost = $this->objFromFixture(BlogPost::class, 'FirstBlogPost');
$archiveLink = $blogPost->getMonthlyArchiveLink($type);
$this->assertContains('archive/', $archiveLink);
$this->assertContains($expected, $archiveLink);
}
/**
* @return array[]
*/
public function monthlyArchiveLinkProvider()
{
return [
['day', '/2013/10/1'],
['month', '/2013/10'],
['year', '/2013'],
];
}
public function testGetYearlyArchiveLink()
{
/** @var BlogPost $blogPost */
$blogPost = $this->objFromFixture(BlogPost::class, 'FirstBlogPost');
$archiveLink = $blogPost->getYearlyArchiveLink();
$this->assertContains('archive/', $archiveLink);
$this->assertContains('/2013', $archiveLink);
}
} }