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

BlogCategoryTest   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 160
Duplicated Lines 63.13 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 9
dl 101
loc 160
rs 10
c 0
b 0
f 0

8 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 11 1
A testCanEdit() 22 22 1
A testCanCreate() 12 12 1
A testCanDelete() 20 20 1

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\BlogCategory;
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
15
/**
16
 * @mixin PHPUnit_Framework_TestCase
17
 */
18
class BlogCategoryTest extends FunctionalTest
19
{
20
    /**
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 $category->BlogPosts() many_many are published,
47
     * both by 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 BlogCategory $category
61
         */
62
        $category = $this->objFromFixture(BlogCategory::class, 'FirstCategory');
63
64
        $this->assertEquals(5, $category->BlogPosts()->count(), 'Category 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
        $cat = new BlogCategory();
74
        $cat->BlogID = $blog->ID;
75
        $cat->Title = 'تست';
76
        $cat->write();
77
        // urlencoded
78
        $this->assertEquals('%D8%AA%D8%B3%D8%AA', $cat->URLSegment);
79
        $link = Controller::join_links($cat->Blog()->Link(), 'category', '%D8%AA%D8%B3%D8%AA');
80
        $this->assertEquals($link, $cat->getLink());
81
    }
82
83
    public function testCanView()
84
    {
85
        $this->useDraftSite();
86
87
        $this->objFromFixture(Member::class, 'Admin');
88
89
        $editor = $this->objFromFixture(Member::class, 'Editor');
90
        $category = $this->objFromFixture(BlogCategory::class, 'SecondCategory');
91
92
        $this->assertFalse($category->canView($editor), 'Editor should not be able to view category.');
0 ignored issues
show
Bug introduced by
It seems like $editor defined by $this->objFromFixture(\S...ember::class, 'Editor') on line 89 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...
93
    }
94
95
    /**
96
     * The first blog can be viewed by anybody.
97
     */
98 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...
99
    {
100
        $this->useDraftSite();
101
102
        $admin = $this->objFromFixture(Member::class, 'Admin');
103
        $editor = $this->objFromFixture(Member::class, 'Editor');
104
105
        $category = $this->objFromFixture(BlogCategory::class, 'FirstCategory');
106
107
        $this->assertTrue($category->canEdit($admin), 'Admin should be able to edit category.');
0 ignored issues
show
Bug introduced by
It seems like $admin defined by $this->objFromFixture(\S...Member::class, 'Admin') on line 102 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...
108
        $this->assertTrue($category->canEdit($editor), 'Editor should be able to edit category.');
0 ignored issues
show
Bug introduced by
It seems like $editor defined by $this->objFromFixture(\S...ember::class, 'Editor') on line 103 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...
109
110
        $category = $this->objFromFixture(BlogCategory::class, 'SecondCategory');
111
112
        $this->assertTrue($category->canEdit($admin), 'Admin should be able to edit category.');
0 ignored issues
show
Bug introduced by
It seems like $admin defined by $this->objFromFixture(\S...Member::class, 'Admin') on line 102 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...
113
        $this->assertFalse($category->canEdit($editor), 'Editor should not be able to edit category.');
0 ignored issues
show
Bug introduced by
It seems like $editor defined by $this->objFromFixture(\S...ember::class, 'Editor') on line 103 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
115
        $category = $this->objFromFixture(BlogCategory::class, 'ThirdCategory');
116
117
        $this->assertTrue($category->canEdit($admin), 'Admin should always be able to edit category.');
0 ignored issues
show
Bug introduced by
It seems like $admin defined by $this->objFromFixture(\S...Member::class, 'Admin') on line 102 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...
118
        $this->assertTrue($category->canEdit($editor), 'Editor should be able to edit category.');
0 ignored issues
show
Bug introduced by
It seems like $editor defined by $this->objFromFixture(\S...ember::class, 'Editor') on line 103 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
    }
120
121 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...
122
    {
123
        $this->useDraftSite();
124
125
        $admin = $this->objFromFixture(Member::class, 'Admin');
126
        $editor = $this->objFromFixture(Member::class, 'Editor');
127
128
        $category = singleton(BlogCategory::class);
129
130
        $this->assertTrue($category->canCreate($admin), 'Admin should be able to create category.');
131
        $this->assertTrue($category->canCreate($editor), 'Editor should be able to create category.');
132
    }
133
134 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...
135
    {
136
        $this->useDraftSite();
137
138
        $admin = $this->objFromFixture(Member::class, 'Admin');
139
        $editor = $this->objFromFixture(Member::class, 'Editor');
140
141
        $category = $this->objFromFixture(BlogCategory::class, 'FirstCategory');
142
143
        $this->assertTrue($category->canDelete($admin), 'Admin should be able to delete category.');
0 ignored issues
show
Bug introduced by
It seems like $admin defined by $this->objFromFixture(\S...Member::class, 'Admin') on line 138 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...
144
        $this->assertTrue($category->canDelete($editor), 'Editor should be able to category category.');
0 ignored issues
show
Bug introduced by
It seems like $editor defined by $this->objFromFixture(\S...ember::class, 'Editor') on line 139 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...
145
146
        $category = $this->objFromFixture(BlogCategory::class, 'SecondCategory');
147
        $this->assertTrue($category->canDelete($admin), 'Admin should be able to delete category.');
0 ignored issues
show
Bug introduced by
It seems like $admin defined by $this->objFromFixture(\S...Member::class, 'Admin') on line 138 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...
148
        $this->assertFalse($category->canDelete($editor), 'Editor should not be able to delete category.');
0 ignored issues
show
Bug introduced by
It seems like $editor defined by $this->objFromFixture(\S...ember::class, 'Editor') on line 139 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...
149
150
        $category = $this->objFromFixture(BlogCategory::class, 'ThirdCategory');
151
        $this->assertTrue($category->canDelete($admin), 'Admin should always be able to delete category.');
0 ignored issues
show
Bug introduced by
It seems like $admin defined by $this->objFromFixture(\S...Member::class, 'Admin') on line 138 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...
152
        $this->assertTrue($category->canDelete($editor), 'Editor should be able to delete category.');
0 ignored issues
show
Bug introduced by
It seems like $editor defined by $this->objFromFixture(\S...ember::class, 'Editor') on line 139 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...
153
    }
154
155 View Code Duplication
    public function testDuplicateCategories()
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...
156
    {
157
        $blog = new Blog();
158
        $blog->Title = 'Testing for duplicate categories';
159
        $blog->write();
160
161
        $category = new BlogCategory();
162
        $category->Title = 'Test';
163
        $category->BlogID = $blog->ID;
164
        $category->URLSegment = 'test';
165
        $category->write();
166
167
        $category = new BlogCategory();
168
        $category->Title = 'Test';
169
        $category->URLSegment = 'test';
170
        $category->BlogID = $blog->ID;
171
        try {
172
            $category->write();
173
            $this->fail('Duplicate BlogCategory written');
174
        } catch (ValidationException $e) {
175
            $messages = $e->getResult()->getMessages();
176
            $this->assertCount(1, $messages);
177
            $this->assertEquals(BlogTag::DUPLICATE_EXCEPTION, $messages[0]['messageType']);
178
        }
179
    }
180
}
181