Completed
Push — master ( 067f41...e7ef92 )
by Damian
02:10
created

tests/BlogTagTest.php (4 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
/**
4
 * @mixin PHPUnit_Framework_TestCase
5
 */
6
class BlogTagTest extends FunctionalTest
7
{
8
    /**
9
     * @var string
10
     */
11
    public static $fixture_file = 'blog.yml';
12
13
    /**
14
     * {@inheritdoc}
15
     */
16
    public function setUp()
17
    {
18
        parent::setUp();
19
20
        SS_Datetime::set_mock_now('2013-10-10 20:00:00');
21
    }
22
23
    /**
24
     * {@inheritdoc}
25
     */
26
    public function tearDown()
27
    {
28
        SS_Datetime::clear_mock_now();
29
30
        parent::tearDown();
31
    }
32
33
    /**
34
     * Tests that any blog posts returned from $tag->BlogPosts() many_many are published, both by
35
     * normal 'save & publish' functionality and by publish date.
36
     */
37 View Code Duplication
    public function testBlogPosts()
38
    {
39
        $member = Member::currentUser();
40
41
        if ($member) {
42
            $member->logout();
43
        }
44
45
        $this->objFromFixture('BlogPost', 'FirstBlogPost');
46
47
        /**
48
         * @var BlogTag $tag
49
         */
50
        $tag = $this->objFromFixture('BlogTag', 'FirstTag');
51
52
        $this->assertEquals(1, $tag->BlogPosts()->count(), 'Tag blog post count');
53
    }
54
55
    /**
56
     * The first blog can be viewed by anybody.
57
     */
58
    public function testCanView()
59
    {
60
        $this->useDraftSite();
61
62
        $admin = $this->objFromFixture('Member', 'Admin');
63
        $editor = $this->objFromFixture('Member', 'Editor');
64
65
        $tag = $this->objFromFixture('BlogTag', 'FirstTag');
66
67
        $this->assertTrue($tag->canView($admin), 'Admin should be able to view tag.');
68
        $this->assertTrue($tag->canView($editor), 'Editor should be able to view tag.');
69
70
        $tag = $this->objFromFixture('BlogTag', 'SecondTag');
71
72
        $this->assertTrue($tag->canView($admin), 'Admin should be able to view tag.');
73
        $this->assertFalse($tag->canView($editor), 'Editor should not be able to view tag.');
74
    }
75
76 View Code Duplication
    public function testCanEdit()
77
    {
78
        $this->useDraftSite();
79
80
        $admin = $this->objFromFixture('Member', 'Admin');
81
        $editor = $this->objFromFixture('Member', 'Editor');
82
83
        $tag = $this->objFromFixture('BlogTag', 'FirstTag');
84
85
        $this->assertTrue($tag->canEdit($admin), 'Admin should be able to edit tag.');
86
        $this->assertTrue($tag->canEdit($editor), 'Editor should be able to edit tag.');
87
88
        $tag = $this->objFromFixture('BlogTag', 'SecondTag');
89
90
        $this->assertTrue($tag->canEdit($admin), 'Admin should be able to edit tag.');
91
        $this->assertFalse($tag->canEdit($editor), 'Editor should not be able to edit tag.');
92
93
        $tag = $this->objFromFixture('BlogTag', 'ThirdTag');
94
95
        $this->assertTrue($tag->canEdit($admin), 'Admin should always be able to edit tags.');
96
        $this->assertTrue($tag->canEdit($editor), 'Editor should be able to edit tag.');
97
    }
98
99 View Code Duplication
    public function testCanCreate()
100
    {
101
        $this->useDraftSite();
102
103
        $admin = $this->objFromFixture('Member', 'Admin');
104
        $editor = $this->objFromFixture('Member', 'Editor');
105
106
        $tag = singleton('BlogTag');
107
108
        $this->assertTrue($tag->canCreate($admin), 'Admin should be able to create tag.');
109
        $this->assertTrue($tag->canCreate($editor), 'Editor should be able to create tag.');
110
    }
111
112 View Code Duplication
    public function testCanDelete()
113
    {
114
        $this->useDraftSite();
115
116
        $admin = $this->objFromFixture('Member', 'Admin');
117
        $editor = $this->objFromFixture('Member', 'Editor');
118
119
        $tag = $this->objFromFixture('BlogTag', 'FirstTag');
120
121
        $this->assertTrue($tag->canDelete($admin), 'Admin should be able to delete tag.');
122
        $this->assertTrue($tag->canDelete($editor), 'Editor should be able to delete tag.');
123
124
        $tag = $this->objFromFixture('BlogTag', 'SecondTag');
125
126
        $this->assertTrue($tag->canDelete($admin), 'Admin should be able to delete tag.');
127
        $this->assertFalse($tag->canDelete($editor), 'Editor should not be able to delete tag.');
128
129
        $tag = $this->objFromFixture('BlogTag', 'ThirdTag');
130
131
        $this->assertTrue($tag->canDelete($admin), 'Admin should always be able to delete tags.');
132
        $this->assertTrue($tag->canDelete($editor), 'Editor should be able to delete tag.');
133
    }
134
135
    public function testDuplicateTagsForURLSegment() {
136
        $blog = new Blog();
137
        $blog->Title = 'Testing for duplicates blog';
138
        $blog->write();
139
        $tag1 = new BlogTag();
140
        $tag1->Title = 'cat-test';
141
        $tag1->BlogID = $blog->ID;
142
        $tag1->write();
143
        $this->assertEquals('cat-test', $tag1->URLSegment);
144
145
        $tag2 = new BlogTag();
146
        $tag2->Title = 'cat test';
147
        $tag2->BlogID = $blog->ID;
148
        $tag2->write();
149
        $this->assertEquals('cat-test-0', $tag2->URLSegment);
150
151
    }
152
153 View Code Duplication
    public function testDuplicateTags() {
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
154
        $blog = new Blog();
155
        $blog->Title = 'Testing for duplicate tags';
156
        $blog->write();
157
158
        $tag = new BlogTag();
159
        $tag->Title = 'Test';
160
        $tag->BlogID = $blog->ID;
161
        $tag->write();
162
163
        $tag = new BlogTag();
164
        $tag->Title = 'Test';
165
        $tag->BlogID = $blog->ID;
166
        try {
167
            $tag->write();
168
            $this->fail('Duplicate BlogTag written');
0 ignored issues
show
The method fail() does not seem to exist on object<BlogTagTest>.

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...
169
        } catch (ValidationException $e) {
170
            $codeList = $e->getResult()->codeList();
171
            $this->assertCount(1, $codeList);
0 ignored issues
show
The method assertCount() does not seem to exist on object<BlogTagTest>.

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...
172
            $this->assertEquals(BlogTag::DUPLICATE_EXCEPTION, $codeList[0]);
0 ignored issues
show
The method assertEquals() does not seem to exist on object<BlogTagTest>.

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...
173
        }
174
    }
175
176
}
177