Completed
Pull Request — master (#421)
by Robbie
02:34
created

BlogTagTest   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 190
Duplicated Lines 57.89 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 9
dl 110
loc 190
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 6 1
A tearDown() 0 6 1
A testBlogPosts() 17 17 2
A testAllowMultibyteUrlSegment() 12 12 1
A testCanView() 0 17 1
A testCanEdit() 22 22 1
A testCanCreate() 12 12 1
A testCanDelete() 22 22 1
A testDuplicateTagsForURLSegment() 0 17 1
B testDuplicateTags() 25 25 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace SilverStripe\Blog\Tests;
4
5
use SilverStripe\Blog\Model\Blog;
6
use SilverStripe\Blog\Model\BlogPost;
7
use SilverStripe\Blog\Model\BlogTag;
8
use SilverStripe\Control\Controller;
9
use SilverStripe\Dev\FunctionalTest;
10
use SilverStripe\ORM\FieldType\DBDatetime;
11
use SilverStripe\ORM\ValidationException;
12
use SilverStripe\Security\Member;
13
14
/**
15
 * @mixin PHPUnit_Framework_TestCase
16
 */
17
class BlogTagTest extends FunctionalTest
18
{
19
    /**
20
     * {@inheritDoc}
21
     * @var string
22
     */
23
    protected static $fixture_file = 'blog.yml';
24
25
    /**
26
     * {@inheritdoc}
27
     */
28
    public function setUp()
29
    {
30
        parent::setUp();
31
32
        DBDatetime::set_mock_now('2013-10-10 20:00:00');
33
    }
34
35
    /**
36
     * {@inheritdoc}
37
     */
38
    public function tearDown()
39
    {
40
        DBDatetime::clear_mock_now();
41
42
        parent::tearDown();
43
    }
44
45
    /**
46
     * Tests that any blog posts returned from $tag->BlogPosts() many_many are published, both by
47
     * normal 'save & publish' functionality and by publish date.
48
     */
49 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...
50
    {
51
        $member = Member::currentUser();
52
53
        if ($member) {
54
            $member->logout();
55
        }
56
57
        $this->objFromFixture(BlogPost::class, 'FirstBlogPost');
58
59
        /**
60
         * @var BlogTag $tag
61
         */
62
        $tag = $this->objFromFixture(BlogTag::class, 'FirstTag');
63
64
        $this->assertEquals(1, $tag->BlogPosts()->count(), 'Tag blog post count');
65
    }
66
67
    /**
68
     * @see https://github.com/silverstripe/silverstripe-blog/issues/376
69
     */
70 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...
71
    {
72
        $blog = $this->objFromFixture(Blog::class, 'FirstBlog');
73
        $tag = new BlogTag();
74
        $tag->BlogID = $blog->ID;
75
        $tag->Title = 'تست';
76
        $tag->write();
77
        // urlencoded
78
        $this->assertEquals('%D8%AA%D8%B3%D8%AA', $tag->URLSegment);
79
        $link = Controller::join_links($tag->Blog()->Link(), 'tag', '%D8%AA%D8%B3%D8%AA');
80
        $this->assertEquals($link, $tag->getLink());
81
    }
82
83
    /**
84
     * The first blog can be viewed by anybody.
85
     */
86
    public function testCanView()
87
    {
88
        $this->useDraftSite();
89
90
        $admin = $this->objFromFixture(Member::class, 'Admin');
91
        $editor = $this->objFromFixture(Member::class, 'Editor');
92
93
        $tag = $this->objFromFixture(BlogTag::class, 'FirstTag');
94
95
        $this->assertTrue($tag->canView($admin), 'Admin should be able to view tag.');
0 ignored issues
show
Bug introduced by
It seems like $admin defined by $this->objFromFixture(\S...Member::class, 'Admin') on line 90 can also be of type object<SilverStripe\ORM\DataObject>; however, SilverStripe\ORM\DataObject::canView() does only seem to accept object<SilverStripe\Security\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...
96
        $this->assertTrue($tag->canView($editor), 'Editor should be able to view tag.');
0 ignored issues
show
Bug introduced by
It seems like $editor defined by $this->objFromFixture(\S...ember::class, 'Editor') on line 91 can also be of type object<SilverStripe\ORM\DataObject>; however, SilverStripe\ORM\DataObject::canView() does only seem to accept object<SilverStripe\Security\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...
97
98
        $tag = $this->objFromFixture(BlogTag::class, 'SecondTag');
99
100
        $this->assertTrue($tag->canView($admin), 'Admin should be able to view tag.');
0 ignored issues
show
Bug introduced by
It seems like $admin defined by $this->objFromFixture(\S...Member::class, 'Admin') on line 90 can also be of type object<SilverStripe\ORM\DataObject>; however, SilverStripe\ORM\DataObject::canView() does only seem to accept object<SilverStripe\Security\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...
101
        $this->assertFalse($tag->canView($editor), 'Editor should not be able to view tag.');
0 ignored issues
show
Bug introduced by
It seems like $editor defined by $this->objFromFixture(\S...ember::class, 'Editor') on line 91 can also be of type object<SilverStripe\ORM\DataObject>; however, SilverStripe\ORM\DataObject::canView() does only seem to accept object<SilverStripe\Security\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...
102
    }
103
104 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...
105
    {
106
        $this->useDraftSite();
107
108
        $admin = $this->objFromFixture(Member::class, 'Admin');
109
        $editor = $this->objFromFixture(Member::class, 'Editor');
110
111
        $tag = $this->objFromFixture(BlogTag::class, 'FirstTag');
112
113
        $this->assertTrue($tag->canEdit($admin), 'Admin should be able to edit tag.');
0 ignored issues
show
Bug introduced by
It seems like $admin defined by $this->objFromFixture(\S...Member::class, 'Admin') on line 108 can also be of type object<SilverStripe\ORM\DataObject>; however, SilverStripe\ORM\DataObject::canEdit() does only seem to accept object<SilverStripe\Security\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...
114
        $this->assertTrue($tag->canEdit($editor), 'Editor should be able to edit tag.');
0 ignored issues
show
Bug introduced by
It seems like $editor defined by $this->objFromFixture(\S...ember::class, 'Editor') on line 109 can also be of type object<SilverStripe\ORM\DataObject>; however, SilverStripe\ORM\DataObject::canEdit() does only seem to accept object<SilverStripe\Security\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...
115
116
        $tag = $this->objFromFixture(BlogTag::class, 'SecondTag');
117
118
        $this->assertTrue($tag->canEdit($admin), 'Admin should be able to edit tag.');
0 ignored issues
show
Bug introduced by
It seems like $admin defined by $this->objFromFixture(\S...Member::class, 'Admin') on line 108 can also be of type object<SilverStripe\ORM\DataObject>; however, SilverStripe\ORM\DataObject::canEdit() does only seem to accept object<SilverStripe\Security\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...
119
        $this->assertFalse($tag->canEdit($editor), 'Editor should not be able to edit tag.');
0 ignored issues
show
Bug introduced by
It seems like $editor defined by $this->objFromFixture(\S...ember::class, 'Editor') on line 109 can also be of type object<SilverStripe\ORM\DataObject>; however, SilverStripe\ORM\DataObject::canEdit() does only seem to accept object<SilverStripe\Security\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...
120
121
        $tag = $this->objFromFixture(BlogTag::class, 'ThirdTag');
122
123
        $this->assertTrue($tag->canEdit($admin), 'Admin should always be able to edit tags.');
0 ignored issues
show
Bug introduced by
It seems like $admin defined by $this->objFromFixture(\S...Member::class, 'Admin') on line 108 can also be of type object<SilverStripe\ORM\DataObject>; however, SilverStripe\ORM\DataObject::canEdit() does only seem to accept object<SilverStripe\Security\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...
124
        $this->assertTrue($tag->canEdit($editor), 'Editor should be able to edit tag.');
0 ignored issues
show
Bug introduced by
It seems like $editor defined by $this->objFromFixture(\S...ember::class, 'Editor') on line 109 can also be of type object<SilverStripe\ORM\DataObject>; however, SilverStripe\ORM\DataObject::canEdit() does only seem to accept object<SilverStripe\Security\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...
125
    }
126
127 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...
128
    {
129
        $this->useDraftSite();
130
131
        $admin = $this->objFromFixture(Member::class, 'Admin');
132
        $editor = $this->objFromFixture(Member::class, 'Editor');
133
134
        $tag = singleton(BlogTag::class);
135
136
        $this->assertTrue($tag->canCreate($admin), 'Admin should be able to create tag.');
137
        $this->assertTrue($tag->canCreate($editor), 'Editor should be able to create tag.');
138
    }
139
140 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...
141
    {
142
        $this->useDraftSite();
143
144
        $admin = $this->objFromFixture(Member::class, 'Admin');
145
        $editor = $this->objFromFixture(Member::class, 'Editor');
146
147
        $tag = $this->objFromFixture(BlogTag::class, 'FirstTag');
148
149
        $this->assertTrue($tag->canDelete($admin), 'Admin should be able to delete tag.');
0 ignored issues
show
Bug introduced by
It seems like $admin defined by $this->objFromFixture(\S...Member::class, 'Admin') on line 144 can also be of type object<SilverStripe\ORM\DataObject>; however, SilverStripe\ORM\DataObject::canDelete() does only seem to accept object<SilverStripe\Security\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...
150
        $this->assertTrue($tag->canDelete($editor), 'Editor should be able to delete tag.');
0 ignored issues
show
Bug introduced by
It seems like $editor defined by $this->objFromFixture(\S...ember::class, 'Editor') on line 145 can also be of type object<SilverStripe\ORM\DataObject>; however, SilverStripe\ORM\DataObject::canDelete() does only seem to accept object<SilverStripe\Security\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...
151
152
        $tag = $this->objFromFixture(BlogTag::class, 'SecondTag');
153
154
        $this->assertTrue($tag->canDelete($admin), 'Admin should be able to delete tag.');
0 ignored issues
show
Bug introduced by
It seems like $admin defined by $this->objFromFixture(\S...Member::class, 'Admin') on line 144 can also be of type object<SilverStripe\ORM\DataObject>; however, SilverStripe\ORM\DataObject::canDelete() does only seem to accept object<SilverStripe\Security\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...
155
        $this->assertFalse($tag->canDelete($editor), 'Editor should not be able to delete tag.');
0 ignored issues
show
Bug introduced by
It seems like $editor defined by $this->objFromFixture(\S...ember::class, 'Editor') on line 145 can also be of type object<SilverStripe\ORM\DataObject>; however, SilverStripe\ORM\DataObject::canDelete() does only seem to accept object<SilverStripe\Security\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...
156
157
        $tag = $this->objFromFixture(BlogTag::class, 'ThirdTag');
158
159
        $this->assertTrue($tag->canDelete($admin), 'Admin should always be able to delete tags.');
0 ignored issues
show
Bug introduced by
It seems like $admin defined by $this->objFromFixture(\S...Member::class, 'Admin') on line 144 can also be of type object<SilverStripe\ORM\DataObject>; however, SilverStripe\ORM\DataObject::canDelete() does only seem to accept object<SilverStripe\Security\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...
160
        $this->assertTrue($tag->canDelete($editor), 'Editor should be able to delete tag.');
0 ignored issues
show
Bug introduced by
It seems like $editor defined by $this->objFromFixture(\S...ember::class, 'Editor') on line 145 can also be of type object<SilverStripe\ORM\DataObject>; however, SilverStripe\ORM\DataObject::canDelete() does only seem to accept object<SilverStripe\Security\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...
161
    }
162
163
    public function testDuplicateTagsForURLSegment()
164
    {
165
        $blog = new Blog();
166
        $blog->Title = 'Testing for duplicates blog';
167
        $blog->write();
168
        $tag1 = new BlogTag();
169
        $tag1->Title = 'cat-test';
170
        $tag1->BlogID = $blog->ID;
171
        $tag1->write();
172
        $this->assertEquals('cat-test', $tag1->URLSegment);
173
174
        $tag2 = new BlogTag();
175
        $tag2->Title = 'cat test';
176
        $tag2->BlogID = $blog->ID;
177
        $tag2->write();
178
        $this->assertEquals('cat-test-1', $tag2->URLSegment);
179
    }
180
181 View Code Duplication
    public function testDuplicateTags()
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...
182
    {
183
        $blog = new Blog();
184
        $blog->Title = 'Testing for duplicate tags';
185
        $blog->write();
186
187
        $tag = new BlogTag();
188
        $tag->Title = 'Test';
189
        $tag->BlogID = $blog->ID;
190
        $tag->URLSegment = 'test';
191
        $tag->write();
192
193
        $tag = new BlogTag();
194
        $tag->Title = 'Test';
195
        $tag->URLSegment = 'test';
196
        $tag->BlogID = $blog->ID;
197
        try {
198
            $tag->write();
199
            $this->fail('Duplicate BlogTag written');
200
        } catch (ValidationException $e) {
201
            $messages = $e->getResult()->getMessages();
202
            $this->assertCount(1, $messages);
203
            $this->assertEquals(BlogTag::DUPLICATE_EXCEPTION, $messages[0]['messageType']);
204
        }
205
    }
206
}
207