Completed
Pull Request — master (#373)
by Michael
02:14
created

BlogPostTest   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4
Metric Value
wmc 6
lcom 1
cbo 4
dl 0
loc 94
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 4 1
A tearDown() 0 5 1
A testCanView() 0 7 1
B canViewProvider() 0 32 1
A testCandidateAuthors() 0 15 1
A testCanViewFuturePost() 0 9 1
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
        $blogPost->PublishDate = null;
91
92
        $editor = $this->objFromFixture('Member', 'BlogEditor');
93
        $this->assertTrue($blogPost->canView($editor));
0 ignored issues
show
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...
Bug introduced by
It seems like $editor defined by $this->objFromFixture('Member', 'BlogEditor') on line 92 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...
94
        $this->assertFalse($blogPost->canView());
0 ignored issues
show
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...
95
    }
96
}
97