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

BlogCategoryTest::testDuplicateCategories()   B

Complexity

Conditions 2
Paths 3

Size

Total Lines 25
Code Lines 20

Duplication

Lines 25
Ratio 100 %

Importance

Changes 0
Metric Value
dl 25
loc 25
rs 8.8571
c 0
b 0
f 0
cc 2
eloc 20
nc 3
nop 0
1
<?php
2
3
use SilverStripe\Blog\Model\Blog;
4
use SilverStripe\Blog\Model\BlogCategory;
5
use SilverStripe\Blog\Model\BlogTag;
6
use SilverStripe\Dev\FunctionalTest;
7
use SilverStripe\ORM\FieldType\DBDatetime;
8
use SilverStripe\ORM\ValidationException;
9
use SilverStripe\Security\Member;
10
11
/**
12
 * @mixin PHPUnit_Framework_TestCase
13
 */
14
class BlogCategoryTest extends FunctionalTest
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...
15
{
16
    /**
17
     * @var string
18
     */
19
    public static $fixture_file = 'blog.yml';
20
21
    /**
22
     * {@inheritdoc}
23
     */
24
    public function setUp()
25
    {
26
        parent::setUp();
27
28
        DBDatetime::set_mock_now('2013-10-10 20:00:00');
29
    }
30
31
    /**
32
     * {@inheritdoc}
33
     */
34
    public function tearDown()
35
    {
36
        DBDatetime::clear_mock_now();
37
38
        parent::tearDown();
39
    }
40
41
    /**
42
     * Tests that any blog posts returned from $category->BlogPosts() many_many are published,
43
     * both by normal 'save & publish' functionality and by publish date.
44
     */
45 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...
46
    {
47
        $member = Member::currentUser();
48
49
        if ($member) {
50
            $member->logout();
51
        }
52
53
        $this->objFromFixture('SilverStripe\\Blog\\Model\\BlogPost', 'FirstBlogPost');
54
55
        /**
56
         * @var BlogCategory $category
57
         */
58
        $category = $this->objFromFixture('SilverStripe\\Blog\\Model\\BlogCategory', 'FirstCategory');
59
60
        $this->assertEquals(5, $category->BlogPosts()->count(), 'Category blog post count');
61
    }
62
63
    /**
64
     * @see https://github.com/silverstripe/silverstripe-blog/issues/376
65
     */
66 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...
67
    {
68
        $blog = $this->objFromFixture('Blog', 'FirstBlog');
69
        $cat = new BlogCategory();
70
        $cat->BlogID = $blog->ID;
71
        $cat->Title = 'تست';
72
        $cat->write();
73
        // urlencoded
74
        $this->assertEquals('%D8%AA%D8%B3%D8%AA', $cat->URLSegment);
75
        $link = Controller::join_links($cat->Blog()->Link(), 'category', '%D8%AA%D8%B3%D8%AA');
76
        $this->assertEquals($link, $cat->getLink());
77
    }
78
79
    public function testCanView()
80
    {
81
        $this->useDraftSite();
82
83
        $this->objFromFixture('SilverStripe\\Security\\Member', 'Admin');
84
85
        $editor = $this->objFromFixture('SilverStripe\\Security\\Member', 'Editor');
86
        $category = $this->objFromFixture('SilverStripe\\Blog\\Model\\BlogCategory', 'SecondCategory');
87
88
        $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...ity\\Member', 'Editor') on line 85 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...
89
    }
90
91
    /**
92
     * The first blog can be viewed by anybody.
93
     */
94 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...
95
    {
96
        $this->useDraftSite();
97
98
        $admin = $this->objFromFixture('SilverStripe\\Security\\Member', 'Admin');
99
        $editor = $this->objFromFixture('SilverStripe\\Security\\Member', 'Editor');
100
101
        $category = $this->objFromFixture('SilverStripe\\Blog\\Model\\BlogCategory', 'FirstCategory');
102
103
        $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...rity\\Member', 'Admin') on line 98 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...
104
        $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...ity\\Member', 'Editor') on line 99 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...
105
106
        $category = $this->objFromFixture('SilverStripe\\Blog\\Model\\BlogCategory', 'SecondCategory');
107
108
        $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...rity\\Member', 'Admin') on line 98 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
        $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...ity\\Member', 'Editor') on line 99 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...
110
111
        $category = $this->objFromFixture('SilverStripe\\Blog\\Model\\BlogCategory', 'ThirdCategory');
112
113
        $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...rity\\Member', 'Admin') on line 98 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($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...ity\\Member', 'Editor') on line 99 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
117 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...
118
    {
119
        $this->useDraftSite();
120
121
        $admin = $this->objFromFixture('SilverStripe\\Security\\Member', 'Admin');
122
        $editor = $this->objFromFixture('SilverStripe\\Security\\Member', 'Editor');
123
124
        $category = singleton('SilverStripe\\Blog\\Model\\BlogCategory');
125
126
        $this->assertTrue($category->canCreate($admin), 'Admin should be able to create category.');
127
        $this->assertTrue($category->canCreate($editor), 'Editor should be able to create category.');
128
    }
129
130 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...
131
    {
132
        $this->useDraftSite();
133
134
        $admin = $this->objFromFixture('SilverStripe\\Security\\Member', 'Admin');
135
        $editor = $this->objFromFixture('SilverStripe\\Security\\Member', 'Editor');
136
137
        $category = $this->objFromFixture('SilverStripe\\Blog\\Model\\BlogCategory', 'FirstCategory');
138
139
        $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...rity\\Member', 'Admin') on line 134 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...
140
        $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...ity\\Member', 'Editor') on line 135 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...
141
142
        $category = $this->objFromFixture('SilverStripe\\Blog\\Model\\BlogCategory', 'SecondCategory');
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...rity\\Member', 'Admin') on line 134 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->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...ity\\Member', 'Editor') on line 135 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('SilverStripe\\Blog\\Model\\BlogCategory', 'ThirdCategory');
147
        $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...rity\\Member', 'Admin') on line 134 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->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...ity\\Member', 'Editor') on line 135 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
151 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...
152
    {
153
        $blog = new Blog();
154
        $blog->Title = 'Testing for duplicate categories';
155
        $blog->write();
156
157
        $category = new BlogCategory();
158
        $category->Title = 'Test';
159
        $category->BlogID = $blog->ID;
160
        $category->URLSegment = 'test';
161
        $category->write();
162
163
        $category = new BlogCategory();
164
        $category->Title = 'Test';
165
        $category->URLSegment = 'test';
166
        $category->BlogID = $blog->ID;
167
        try {
168
            $category->write();
169
            $this->fail('Duplicate BlogCategory written');
170
        } catch (ValidationException $e) {
171
            $messages = $e->getResult()->getMessages();
172
            $this->assertCount(1, $messages);
173
            $this->assertEquals(BlogTag::DUPLICATE_EXCEPTION, $messages[0]['messageType']);
174
        }
175
    }
176
}
177