BlogTagTest::testCanCreate()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 12

Duplication

Lines 12
Ratio 100 %

Importance

Changes 0
Metric Value
dl 12
loc 12
rs 9.8666
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace SilverStripe\Blog\Tests;
4
5
use PHPUnit_Framework_TestCase;
6
use SilverStripe\Blog\Model\Blog;
7
use SilverStripe\Blog\Model\BlogPost;
8
use SilverStripe\Blog\Model\BlogTag;
9
use SilverStripe\Control\Controller;
10
use SilverStripe\Dev\FunctionalTest;
11
use SilverStripe\ORM\FieldType\DBDatetime;
12
use SilverStripe\ORM\ValidationException;
13
use SilverStripe\Security\Member;
14
use SilverStripe\Security\Security;
15
16
/**
17
 * @mixin PHPUnit_Framework_TestCase
18
 */
19
class BlogTagTest extends FunctionalTest
20
{
21
    protected static $fixture_file = 'blog.yml';
22
23
    protected function setUp()
24
    {
25
        parent::setUp();
26
27
        DBDatetime::set_mock_now('2013-10-10 20:00:00');
28
    }
29
30
    protected function tearDown()
31
    {
32
        DBDatetime::clear_mock_now();
33
34
        parent::tearDown();
35
    }
36
37
    /**
38
     * Tests that any blog posts returned from $tag->BlogPosts() many_many are published, both by
39
     * normal 'save & publish' functionality and by publish date.
40
     */
41 View Code Duplication
    public function testBlogPosts()
0 ignored issues
show
Duplication introduced by
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...
42
    {
43
        $member = Security::getCurrentUser();
44
45
        if ($member) {
46
            Security::setCurrentUser(null);
47
        }
48
49
        $this->objFromFixture(BlogPost::class, 'FirstBlogPost');
50
51
        /**
52
         * @var BlogTag $tag
53
         */
54
        $tag = $this->objFromFixture(BlogTag::class, 'FirstTag');
55
56
        $this->assertEquals(1, $tag->BlogPosts()->count(), 'Tag blog post count');
57
    }
58
59
    /**
60
     * @see https://github.com/silverstripe/silverstripe-blog/issues/376
61
     */
62 View Code Duplication
    public function testAllowMultibyteUrlSegment()
0 ignored issues
show
Duplication introduced by
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...
63
    {
64
        /** @var Blog $blog */
65
        $blog = $this->objFromFixture(Blog::class, 'FirstBlog');
66
        $tag = new BlogTag();
67
        $tag->setBlogID($blog->ID);
68
        $tag->Title = 'تست';
69
        $tag->write();
70
        // urlencoded
71
        $this->assertEquals('%D8%AA%D8%B3%D8%AA', $tag->URLSegment);
72
        $link = Controller::join_links($blog->Link(), 'tag', '%D8%AA%D8%B3%D8%AA');
73
        $this->assertEquals($link, $tag->getLink());
74
    }
75
76
    /**
77
     * The first blog can be viewed by anybody.
78
     */
79
    public function testCanView()
80
    {
81
        $this->useDraftSite();
82
83
        $admin = $this->objFromFixture(Member::class, 'Admin');
84
        $editor = $this->objFromFixture(Member::class, 'Editor');
85
86
        /** @var Blog $firstBlog */
87
        $firstBlog = $this->objFromFixture(Blog::class, 'FirstBlog');
88
        $firstTag = $firstBlog->Tags(false)->find('URLSegment', 'first-tag');
89
90
        $this->assertTrue($firstTag->canView($admin), 'Admin should be able to view tag.');
91
        $this->assertTrue($firstTag->canView($editor), 'Editor should be able to view tag.');
92
93
        /** @var Blog $secondBlog */
94
        $secondBlog = $this->objFromFixture(Blog::class, 'SecondBlog');
95
        $secondTag = $secondBlog->Tags(false)->find('URLSegment', 'second-tag');
96
97
        $this->assertTrue($secondTag->canView($admin), 'Admin should be able to view tag.');
98
        $this->assertFalse($secondTag->canView($editor), 'Editor should not be able to view tag.');
99
    }
100
101 View Code Duplication
    public function testCanEdit()
0 ignored issues
show
Duplication introduced by
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...
102
    {
103
        $this->useDraftSite();
104
105
        $admin = $this->objFromFixture(Member::class, 'Admin');
106
        $editor = $this->objFromFixture(Member::class, 'Editor');
107
108
        /** @var Blog $firstBlog */
109
        $firstBlog = $this->objFromFixture(Blog::class, 'FirstBlog');
110
        $firstTag = $firstBlog->Tags(false)->find('URLSegment', 'first-tag');
111
112
        $this->assertTrue($firstTag->canEdit($admin), 'Admin should be able to edit tag.');
113
        $this->assertTrue($firstTag->canEdit($editor), 'Editor should be able to edit tag.');
114
115
        /** @var Blog $secondBlog */
116
        $secondBlog = $this->objFromFixture(Blog::class, 'SecondBlog');
117
        $secondTag = $secondBlog->Tags(false)->find('URLSegment', 'second-tag');
118
119
        $this->assertTrue($secondTag->canEdit($admin), 'Admin should be able to edit tag.');
120
        $this->assertFalse($secondTag->canEdit($editor), 'Editor should not be able to edit tag.');
121
122
        /** @var Blog $thirdBlog */
123
        $thirdBlog = $this->objFromFixture(Blog::class, 'ThirdBlog');
124
        $thirdTag = $thirdBlog->Tags(false)->find('URLSegment', 'third-tag');
125
126
        $this->assertTrue($thirdTag->canEdit($admin), 'Admin should always be able to edit tags.');
127
        $this->assertTrue($thirdTag->canEdit($editor), 'Editor should be able to edit tag.');
128
    }
129
130 View Code Duplication
    public function testCanCreate()
0 ignored issues
show
Duplication introduced by
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...
131
    {
132
        $this->useDraftSite();
133
134
        $admin = $this->objFromFixture(Member::class, 'Admin');
135
        $editor = $this->objFromFixture(Member::class, 'Editor');
136
137
        $tag = singleton(BlogTag::class);
138
139
        $this->assertTrue($tag->canCreate($admin), 'Admin should be able to create tag.');
140
        $this->assertTrue($tag->canCreate($editor), 'Editor should be able to create tag.');
141
    }
142
143 View Code Duplication
    public function testCanDelete()
0 ignored issues
show
Duplication introduced by
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...
144
    {
145
        $this->useDraftSite();
146
147
        $admin = $this->objFromFixture(Member::class, 'Admin');
148
        $editor = $this->objFromFixture(Member::class, 'Editor');
149
150
        /** @var Blog $firstBlog */
151
        $firstBlog = $this->objFromFixture(Blog::class, 'FirstBlog');
152
        $firstTag = $firstBlog->Tags(false)->find('URLSegment', 'first-tag');
153
154
        $this->assertTrue($firstTag->canDelete($admin), 'Admin should be able to delete tag.');
155
        $this->assertTrue($firstTag->canDelete($editor), 'Editor should be able to delete tag.');
156
157
        /** @var Blog $secondBlog */
158
        $secondBlog = $this->objFromFixture(Blog::class, 'SecondBlog');
159
        $secondTag = $secondBlog->Tags(false)->find('URLSegment', 'second-tag');
160
161
        $this->assertTrue($secondTag->canDelete($admin), 'Admin should be able to delete tag.');
162
        $this->assertFalse($secondTag->canDelete($editor), 'Editor should not be able to delete tag.');
163
164
        /** @var Blog $thirdBlog */
165
        $thirdBlog = $this->objFromFixture(Blog::class, 'ThirdBlog');
166
        $thirdTag = $thirdBlog->Tags(false)->find('URLSegment', 'third-tag');
167
168
        $this->assertTrue($thirdTag->canDelete($admin), 'Admin should always be able to delete tags.');
169
        $this->assertTrue($thirdTag->canDelete($editor), 'Editor should be able to delete tag.');
170
    }
171
172
    public function testDuplicateTagsForURLSegment()
173
    {
174
        $blog = new Blog();
175
        $blog->Title = 'Testing for duplicates blog';
176
        $blog->write();
177
        $tag1 = new BlogTag();
178
        $tag1->Title = 'cat-test';
179
        $tag1->BlogID = $blog->ID;
0 ignored issues
show
Documentation introduced by
The property BlogID does not exist on object<SilverStripe\Blog\Model\BlogTag>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
180
        $tag1->write();
181
        $this->assertEquals('cat-test', $tag1->URLSegment);
182
183
        $tag2 = new BlogTag();
184
        $tag2->Title = 'cat test';
185
        $tag2->BlogID = $blog->ID;
0 ignored issues
show
Documentation introduced by
The property BlogID does not exist on object<SilverStripe\Blog\Model\BlogTag>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
186
        $tag2->write();
187
        $this->assertEquals('cat-test-1', $tag2->URLSegment);
188
    }
189
190
    public function testDuplicateTags()
191
    {
192
        $blog = new Blog();
193
        $blog->Title = 'Testing for duplicate tags';
194
        $blog->write();
195
196
        $tag = new BlogTag();
197
        $tag->Title = 'Test';
198
        $tag->BlogID = $blog->ID;
0 ignored issues
show
Documentation introduced by
The property BlogID does not exist on object<SilverStripe\Blog\Model\BlogTag>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
199
        $tag->URLSegment = 'test';
200
        $tag->write();
201
202
        $tag = new BlogTag();
203
        $tag->Title = 'Test';
204
        $tag->URLSegment = 'test';
205
        $tag->BlogID = $blog->ID;
0 ignored issues
show
Documentation introduced by
The property BlogID does not exist on object<SilverStripe\Blog\Model\BlogTag>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
206
        try {
207
            $tag->write();
208
            $this->fail('Duplicate BlogTag written');
209
        } catch (ValidationException $e) {
210
            $messages = $e->getResult()->getMessages();
211
            $this->assertCount(1, $messages);
212
            $this->assertEquals(BlogTag::DUPLICATE_EXCEPTION, $messages[0]['messageType']);
213
        }
214
    }
215
216
    public function testBlogTagUrlSegmentsAreAutomaticallyUpdated()
217
    {
218
        $tag = new BlogTag;
219
        $tag->Title = "a test";
220
        $tag->write();
221
        $this->assertEquals($tag->URLSegment, "a-test");
222
223
        $tag->Title = "another test";
224
        $tag->write();
225
        $this->assertEquals($tag->URLSegment, "another-test");
226
    }
227
}
228