Skip to content

Commit

Permalink
Increase test coverage of PaginatedList and BlogPosts
Browse files Browse the repository at this point in the history
  • Loading branch information
cpenny authored and chrispenny committed Jan 6, 2022
1 parent 59bf355 commit 5078364
Show file tree
Hide file tree
Showing 6 changed files with 180 additions and 3 deletions.
13 changes: 10 additions & 3 deletions src/Elements/ElementBlogOverview.php
Original file line number Diff line number Diff line change
Expand Up @@ -274,18 +274,25 @@ public function populateDefaults(): void
}

/**
* We'll allow the passing of a Controller for a couple of reasons:
* 1) Provides a new way for you to dictate how a PaginatedList might be provided
* 2) Makes testing much easier...
*
* @param Controller|null $controller
* @return PaginatedList|null
* @throws ValidationException
*/
public function getPaginatedList(): ?PaginatedList
public function getPaginatedList(?Controller $controller = null): ?PaginatedList
{
// Return our cached value, if one exists
if ($this->paginatedList !== null) {
return $this->paginatedList;
}

/** @var BlogController $controller */
$controller = Controller::curr();
if ($controller === null) {
/** @var BlogController $controller */
$controller = Controller::curr();
}

// Ideally, we want to fetch the PaginatedList from the BlogController, but, if this Block is not being used on
// a Blog page, then that will not be (immediately) possible. You have three options:
Expand Down
101 changes: 101 additions & 0 deletions tests/Elements/ElementBlogOverviewFunctionalTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
<?php

namespace Dynamic\Elements\Blog\Tests\Elements;

use DNADesign\Elemental\Extensions\ElementalPageExtension;
use Dynamic\Elements\Blog\Elements\ElementBlogOverview;
use Dynamic\Elements\Blog\Tests\Fake\PageController;
use Page;
use PageController as BasePageController;
use SilverStripe\Blog\Model\BlogController;
use SilverStripe\Dev\SapphireTest;
use SilverStripe\ORM\ValidationException;
use SilverStripe\Widgets\Extensions\WidgetPageExtension;

/**
* Class ElementBlogOverviewFunctionalTest
*
* @package Dynamic\Elements\Blog\Tests\Elements
*/
class ElementBlogOverviewFunctionalTest extends SapphireTest
{
/**
* @var string
*/
protected static $fixture_file = 'ElementBlogOverviewFunctionalTest.yml';

/**
* @var bool
*/
protected static $use_draft_site = true;

/**
* @var array
*/
protected static $required_extensions = [
Page::class => [
WidgetPageExtension::class,
ElementalPageExtension::class,
],
];

/**
* @throws ValidationException
*/
public function testPaginatedListAvailable(): void
{
/** @var ElementBlogOverview $block */
$block = $this->objFromFixture(ElementBlogOverview::class, 'block1');
$controller = new BlogController();

// Pass the controller through for the first get
$this->assertNotNull($block->getPaginatedList($controller));
// Our cache should now be set, so we shouldn't need the controller this time
$this->assertNotNull($block->getPaginatedList());
}

/**
* @throws ValidationException
*/
public function testPaginatedListDenied(): void
{
/** @var ElementBlogOverview $block */
$block = $this->objFromFixture(ElementBlogOverview::class, 'block2');
$controller = new PageController();

// We have not allowed usage outside of BlogController, so this should be null
$this->assertNull($block->getPaginatedList($controller));
}

/**
* @throws ValidationException
*/
public function testPaginatedListCustom(): void
{
// Update our config to allow this Block type to be used outside of the Blog
ElementBlogOverview::config()->update('allow_use_outside_of_blog', true);

/** @var ElementBlogOverview $block */
$block = $this->objFromFixture(ElementBlogOverview::class, 'block2');
$controller = new PageController();

// We have not allowed usage outside of BlogController, so this should be null
$this->assertNotNull($block->getPaginatedList($controller));
}

/**
* @throws ValidationException
*/
public function testPaginatedListDefault(): void
{
// Update our config to allow this Block type to be used outside of the Blog
ElementBlogOverview::config()->update('allow_use_outside_of_blog', true);

/** @var ElementBlogOverview $block */
$block = $this->objFromFixture(ElementBlogOverview::class, 'block2');
$controller = new BasePageController();

// We have not allowed usage outside of BlogController, so this should be null
$this->assertNotNull($block->getPaginatedList($controller));
}
}
30 changes: 30 additions & 0 deletions tests/Elements/ElementBlogOverviewFunctionalTest.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
DNADesign\Elemental\Models\ElementalArea:
blog1:
OwnerClassName: SilverStripe\Blog\Model\Blog
fakePage1:
OwnerClassName: Dynamic\Elements\Blog\Tests\Fake\Page

SilverStripe\Blog\Model\Blog:
blog1:
Title: Blog 1
URLSegment: blog1
ElementalAreaID: =>DNADesign\Elemental\Models\ElementalArea.blog1

SilverStripe\Blog\Model\BlogPost:
post1:
Title: Post 1
URLSegment: post1
ParentID: =>SilverStripe\Blog\Model\Blog.blog1

Dynamic\Elements\Blog\Tests\Fake\Page:
fakePage1:
Title: Fake Page 1
ElementalAreaID: =>DNADesign\Elemental\Models\ElementalArea.fakePage1

Dynamic\Elements\Blog\Elements\ElementBlogOverview:
block1:
Title: Overview block 1
ParentID: =>DNADesign\Elemental\Models\ElementalArea.blog1
block2:
Title: Overview block 2
ParentID: =>DNADesign\Elemental\Models\ElementalArea.fakePage1
11 changes: 11 additions & 0 deletions tests/Elements/ElementBlogOverviewTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,17 @@ public function testSideBarCustom(): void
$this->assertNotNull($block->SideBarView());
}

/**
* @throws ValidationException
*/
public function testSideBarNoParent(): void
{
/** @var ElementBlogOverview $block */
$block = $this->objFromFixture(ElementBlogOverview::class, 'block5');

$this->assertNull($block->SideBarView());
}

/**
* @throws ValidationException
*/
Expand Down
2 changes: 2 additions & 0 deletions tests/Elements/ElementBlogOverviewTest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ Dynamic\Elements\Blog\Elements\ElementBlogOverview:
block4:
Title: Overview block 4
ParentID: =>DNADesign\Elemental\Models\ElementalArea.fakePage1
block5:
Title: Overview block 5 (orphan)

SilverStripe\Blog\Widgets\BlogCategoriesWidget:
widget1:
Expand Down
26 changes: 26 additions & 0 deletions tests/Fake/PageController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Dynamic\Elements\Blog\Tests\Fake;

use PageController as BasePageController;
use SilverStripe\Dev\TestOnly;
use SilverStripe\ORM\ArrayList;
use SilverStripe\ORM\PaginatedList;

/**
* Test only class used to test that the Overview block can be used outside of a Blog page
*
* Class Page
*
* @package Dynamic\Elements\Blog\Tests\Fake
*/
class PageController extends BasePageController implements TestOnly
{
/**
* @return PaginatedList
*/
public function PaginatedList(): PaginatedList
{
return PaginatedList::create(ArrayList::create());
}
}

0 comments on commit 5078364

Please sign in to comment.