ElementBlogPostsTest::testGetPostsList()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 29
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 21
nc 1
nop 0
dl 0
loc 29
rs 9.584
c 1
b 0
f 0
1
<?php
2
3
namespace Dynamic\Elements\Blog\Elements\Tests;
4
5
use Dynamic\Elements\Blog\Elements\ElementBlogPosts;
6
use SilverStripe\Blog\Model\Blog;
7
use SilverStripe\Blog\Model\BlogCategory;
8
use SilverStripe\Blog\Model\BlogPost;
9
use SilverStripe\Dev\SapphireTest;
10
use SilverStripe\Forms\FieldList;
11
use SilverStripe\ORM\DataList;
12
use SilverStripe\ORM\SS_List;
13
14
class ElementBlogPostsTest extends SapphireTest
15
{
16
    /**
17
     * @var string
18
     */
19
    protected static $fixture_file = 'element-blog-posts.yml';
20
21
    /**
22
     *
23
     */
24
    public function testGetSummary()
25
    {
26
        $object = $this->objFromFixture(ElementBlogPosts::class, 'one');
27
        $count = $object->getPostsList()->count();
28
        $this->assertEquals(
29
            $object->getSummary(),
30
            _t(
31
                BlogPost::class . 'PLURALS',
32
                'A Blog Post|{count} Base Pages',
33
                ['count' => $count]
34
            )
35
        );
36
    }
37
38
    /**
39
     *
40
     */
41
    public function testGetType()
42
    {
43
        $object = $this->objFromFixture(ElementBlogPosts::class, 'one');
44
        $this->assertEquals($object->getType(), 'Blog Posts');
45
    }
46
47
    /**
48
     *
49
     */
50
    public function testGetCMSFields()
51
    {
52
        $object = $this->objFromFixture(ElementBlogPosts::class, 'one');
53
        $fields = $object->getCMSFields();
54
        $this->assertInstanceOf(FieldList::class, $fields);
55
        $this->assertNotNull($fields->dataFieldByName('BlogID'));
56
    }
57
58
    /**
59
     *
60
     */
61
    public function testGetPostsList()
62
    {
63
        $object = $this->objFromFixture(ElementBlogPosts::class, 'one');
64
        $this->compareList(
65
            $object->Blog()->getBlogPosts()->limit($object->Limit),
66
            $object->getPostsList(),
67
            'Should only return blog post assign to blog page'
68
        );
69
70
        $object = $this->objFromFixture(ElementBlogPosts::class, 'targetCategory');
71
        $category = $this->objFromFixture(BlogCategory::class, 'category');
72
        $this->compareList(
73
            $category->BlogPosts()->limit($object->Limit),
74
            $object->getPostsList(),
75
            'Should only return blog post assign to blog category'
76
        );
77
78
        $object = $this->objFromFixture(ElementBlogPosts::class, 'noTarget');
79
        $this->compareList(
80
            BlogPost::get()->limit($object->Limit),
81
            $object->getPostsList(),
82
            'Should return all blog posts'
83
        );
84
85
        $object = $this->objFromFixture(ElementBlogPosts::class, 'badTarget');
86
        $this->compareList(
87
            BlogPost::get()->limit($object->Limit),
88
            $object->getPostsList(),
89
            'When ElementalBlogPost is misconfigured shoudl return all blog post'
90
        );
91
    }
92
93
    /**
94
     * Compare entries in $expected to entries in actual
95
     * @param DataList $expected
96
     * @param DataList $actual
97
     * @param string $message
98
     */
99
    private function compareList(DataList $expected, DataList $actual, $message = '')
100
    {
101
        $expectedArray = $expected->map('ID', 'ClassName')->toArray();
102
        $actualArray = $expected->map('ID', 'ClassName')->toArray();
103
        $this->assertEquals($expectedArray, $actualArray, $message);
104
    }
105
}
106