Completed
Push — master ( c1ae82...09bdf6 )
by Daniel
9s
created

BlogPostTest::testGetDate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 1
Metric Value
c 1
b 1
f 1
dl 0
loc 8
rs 9.4285
cc 1
eloc 5
nc 1
nop 0
1
<?php
2
3
class BlogPostTest extends SapphireTest
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
4
{
5
    /**
6
     * @var string
7
     */
8
    public static $fixture_file = 'blog.yml';
9
10
    /**
11
     * {@inheritdoc}
12
     */
13
    public function setUp()
14
    {
15
        parent::setUp();
16
    }
17
18
    /**
19
     * {@inheritdoc}
20
     */
21
    public function tearDown()
22
    {
23
        SS_Datetime::clear_mock_now();
24
        parent::tearDown();
25
    }
26
27
    /**
28
     * @dataProvider canViewProvider
29
     */
30
    public function testCanView($date, $user, $page, $canView)
31
    {
32
        $userRecord = $this->objFromFixture('Member', $user);
33
        $pageRecord = $this->objFromFixture('BlogPost', $page);
34
        SS_Datetime::set_mock_now($date);
35
        $this->assertEquals($canView, $pageRecord->canView($userRecord));
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<BlogPostTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Bug introduced by
It seems like $userRecord defined by $this->objFromFixture('Member', $user) on line 32 can also be of type object<DataObject>; however, DataObject::canView() does only seem to accept object<Member>|null, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
36
    }
37
38
    public function canViewProvider()
39
    {
40
        $someFutureDate = '2013-10-10 20:00:00';
41
        $somePastDate = '2009-10-10 20:00:00';
42
        return array(
43
            // Check this post given the date has passed
44
            array($someFutureDate, 'Editor', 'PostA', true),
45
            array($someFutureDate, 'Contributor', 'PostA', true),
46
            array($someFutureDate, 'BlogEditor', 'PostA', true),
47
            array($someFutureDate, 'Writer', 'PostA', true),
48
49
            // Check unpublished pages
50
            array($somePastDate, 'Editor', 'PostA', true),
51
            array($somePastDate, 'Contributor', 'PostA', true),
52
            array($somePastDate, 'BlogEditor', 'PostA', true),
53
            array($somePastDate, 'Writer', 'PostA', true),
54
55
            // Test a page that was authored by another user
56
57
            // Check this post given the date has passed
58
            array($someFutureDate, 'Editor', 'FirstBlogPost', true),
59
            array($someFutureDate, 'Contributor', 'FirstBlogPost', true),
60
            array($someFutureDate, 'BlogEditor', 'FirstBlogPost', true),
61
            array($someFutureDate, 'Writer', 'FirstBlogPost', true),
62
63
            // Check future pages - non-editors shouldn't be able to see this
64
            array($somePastDate, 'Editor', 'FirstBlogPost', true),
65
            array($somePastDate, 'Contributor', 'FirstBlogPost', false),
66
            array($somePastDate, 'BlogEditor', 'FirstBlogPost', false),
67
            array($somePastDate, 'Writer', 'FirstBlogPost', false),
68
        );
69
    }
70
71
    public function testCandidateAuthors()
72
    {
73
        $blogpost = $this->objFromFixture('BlogPost', 'PostC');
74
75
        $this->assertEquals(7, $blogpost->getCandidateAuthors()->count());
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<BlogPostTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
76
77
        //Set the group to draw Members from
78
        Config::inst()->update('BlogPost', 'restrict_authors_to_group', 'blogusers');
79
80
        $this->assertEquals(3, $blogpost->getCandidateAuthors()->count());
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<BlogPostTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
81
82
        // Test cms field is generated
83
        $fields = $blogpost->getCMSFields();
84
        $this->assertNotEmpty($fields->dataFieldByName('Authors'));
0 ignored issues
show
Bug introduced by
The method assertNotEmpty() does not seem to exist on object<BlogPostTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
85
    }
86
87
    public function testCanViewFuturePost()
88
    {
89
        $blogPost = $this->objFromFixture('BlogPost', 'NullPublishDate');
90
91
        $editor = $this->objFromFixture('Member', 'BlogEditor');
92
        $this->assertTrue($blogPost->canView($editor));
0 ignored issues
show
Bug introduced by
It seems like $editor defined by $this->objFromFixture('Member', 'BlogEditor') on line 91 can also be of type object<DataObject>; however, DataObject::canView() does only seem to accept object<Member>|null, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
Bug introduced by
The method assertTrue() does not seem to exist on object<BlogPostTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
93
94
        $visitor = $this->objFromFixture('Member', 'Visitor');
95
        $this->assertFalse($blogPost->canView($visitor));
0 ignored issues
show
Bug introduced by
It seems like $visitor defined by $this->objFromFixture('Member', 'Visitor') on line 94 can also be of type object<DataObject>; however, DataObject::canView() does only seem to accept object<Member>|null, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
Bug introduced by
The method assertFalse() does not seem to exist on object<BlogPostTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
96
    }
97
98
    /**
99
     * The purpose of getDate() is to act as a proxy for PublishDate in the default RSS
100
     * template, rather than copying the entire template.
101
     */
102
    public function testGetDate()
103
    {
104
        $blogPost = $this->objFromFixture('BlogPost', 'NullPublishDate');
105
        $this->assertNull($blogPost->getDate());
0 ignored issues
show
Bug introduced by
The method assertNull() does not seem to exist on object<BlogPostTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
106
107
        $blogPost = $this->objFromFixture('BlogPost', 'PostA');
108
        $this->assertEquals('2012-01-09 15:00:00', $blogPost->getDate());
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<BlogPostTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
109
    }
110
}
111