Completed
Push — master ( 118de6...3501ef )
by
unknown
10s
created

testGetColumnsHandled()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 0
1
<?php
2
3
namespace SilverStripe\Comments\Tests;
4
5
use SilverStripe\Comments\Admin\CommentAdmin;
6
use SilverStripe\Comments\Admin\CommentsGridField;
7
use SilverStripe\Comments\Admin\CommentsGridFieldAction;
8
use SilverStripe\Comments\Admin\CommentsGridFieldConfig;
9
use SilverStripe\Comments\Model\Comment;
10
use SilverStripe\Comments\Tests\Stubs\CommentableItem;
11
use SilverStripe\Control\Controller;
12
use SilverStripe\Dev\SapphireTest;
13
use SilverStripe\Forms\FieldList;
14
use SilverStripe\Forms\Form;
15
use SilverStripe\Forms\GridField\GridField;
16
use SilverStripe\Forms\GridField\GridFieldDeleteAction;
17
use SilverStripe\Forms\Tests\GridField\GridFieldTest\Team;
18
use SilverStripe\ORM\ArrayList;
19
use SilverStripe\ORM\DataList;
20
use SilverStripe\ORM\DataObject;
21
22
class CommentsGridFieldActionTest extends SapphireTest
23
{
24
    protected $usesDatabase = true;
25
26
    protected static $extra_dataobjects = [
27
        CommentableItem::class,
28
    ];
29
30
    /** @var ArrayList */
31
    protected $list;
32
33
    /** @var GridField */
34
    protected $gridField;
35
36
    /** @var Form */
37
    protected $form;
38
39
    protected function setUp()
40
    {
41
        parent::setUp();
42
        $this->list = new DataList(Team::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like new \SilverStripe\ORM\Da...dFieldTest\Team::class) of type object<SilverStripe\ORM\DataList> is incompatible with the declared type object<SilverStripe\ORM\ArrayList> of property $list.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
43
        $config = CommentsGridFieldConfig::create()->addComponent(new GridFieldDeleteAction());
44
        $this->gridField = new CommentsGridField('testfield', 'testfield', $this->list, $config);
45
        $this->form = new Form(new CommentAdmin(), 'mockform', new FieldList(array($this->gridField)), new FieldList());
46
    }
47
48
    public function testAugmentColumns()
49
    {
50
        $action = new CommentsGridFieldAction();
51
52
        // an entry called 'Actions' is added to the columns array
53
        $columns = array();
54
        $action->augmentColumns($this->gridField, $columns);
55
        $expected = array('Actions');
56
        $this->assertEquals($expected, $columns);
57
58
        $columns = array('Actions');
59
        $action->augmentColumns($this->gridField, $columns);
60
        $expected = array('Actions');
61
        $this->assertEquals($expected, $columns);
62
    }
63
64
    public function testGetColumnAttributes()
65
    {
66
        $action = new CommentsGridFieldAction();
67
        $record = new Comment();
68
        $attrs = $action->getColumnAttributes($this->gridField, $record, Comment::class);
69
        $this->assertEquals(array('class' => 'col-buttons'), $attrs);
70
    }
71
72
    public function testGetColumnMetadata()
73
    {
74
        $action = new CommentsGridFieldAction();
75
        $result = $action->getColumnMetadata($this->gridField, 'Actions');
76
        $this->assertEquals(array('title' => ''), $result);
77
        $result = $action->getColumnMetadata($this->gridField, 'SomethingElse');
78
        $this->assertNull($result);
79
    }
80
81
    public function testGetColumnsHandled()
82
    {
83
        $action = new CommentsGridFieldAction();
84
        $result = $action->getColumnsHandled($this->gridField);
85
        $this->assertEquals(array('Actions'), $result);
86
    }
87
88
    public function testGetColumnContent()
89
    {
90
        $this->logInWithPermission('CMS_ACCESS_CommentAdmin');
91
        $action = new CommentsGridFieldAction();
92
        $record = new Comment();
93
        $record->Name = 'Name of commeter';
94
        $record->Comment = 'This is a comment';
95
        $record->write();
96
        $recordID = $record->ID;
97
        $html = $action->getColumnContent($this->gridField, $record, Comment::class);
98
        $this->assertContains('data-url="admin/comments/mockform/field/testfield', $html);
99
100
        $this->assertContains('value="Spam"', $html);
101
        $this->assertContains('id="action_CustomAction' . $recordID . 'Spam"', $html);
102
103
        $this->assertContains('value="Approve"', $html);
104
        $this->assertContains('id="action_CustomAction' . $recordID . 'Approve"', $html);
105
106
        // If marked as spam, only the approve button should be available
107
        $record->markSpam();
108
        $record->write();
109
        $html = $action->getColumnContent($this->gridField, $record, Comment::class);
110
        $this->assertContains('value="Approve"', $html);
111
        $this->assertNotContains('value="Spam"', $html);
112
113
        // If marked as spam, only the approve button should be available
114
        $record->markApproved();
115
        $record->write();
116
        $html = $action->getColumnContent($this->gridField, $record, Comment::class);
117
        $this->assertNotContains('value="Approve"', $html);
118
        $this->assertContains('value="Spam"', $html);
119
    }
120
121
    public function testGetActions()
122
    {
123
        $action = new CommentsGridFieldAction();
124
        $result = $action->getActions($this->gridField);
125
        $this->assertEquals(array('spam', 'approve'), $result);
126
    }
127
128
    public function testHandleAction()
129
    {
130
        $this->logInWithPermission('CMS_ACCESS_CommentAdmin');
131
        $item = new CommentableItem;
132
        $item->write();
133
134
        $action = new CommentsGridFieldAction();
135
        $record = new Comment();
136
        $record->Name = 'Name of commenter';
137
        $record->Comment = 'This is a comment';
138
        $record->ParentID = $item->ID;
139
        $record->ParentClass = $item->class;
0 ignored issues
show
Documentation introduced by
The property ParentClass does not exist on object<SilverStripe\Comments\Model\Comment>. 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...
Documentation introduced by
The property class does not exist on object<SilverStripe\Comm...\Stubs\CommentableItem>. 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...
140
        $record->write();
141
        $recordID = $record->ID;
142
        $arguments = array('RecordID' => $recordID);
143
        $data = array();
144
        $result = $action->handleAction($this->gridField, 'spam', $arguments, $data);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $result is correct as $action->handleAction($t...am', $arguments, $data) (which targets SilverStripe\Comments\Ad...dAction::handleAction()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
Unused Code introduced by
$result is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
145
        $this->assertEquals(200, Controller::curr()->getResponse()->getStatusCode());
146
        $this->assertEquals(
147
            'Comment marked as spam.',
148
            Controller::curr()->getResponse()->getStatusDescription()
149
        );
150
        $record = DataObject::get_by_id(Comment::class, $recordID);
151
        $this->assertEquals(1, $record->Moderated);
152
        $this->assertEquals(1, $record->IsSpam);
153
154
        //getStatusDescription
155
        $result = $action->handleAction($this->gridField, 'approve', $arguments, $data);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $result is correct as $action->handleAction($t...ve', $arguments, $data) (which targets SilverStripe\Comments\Ad...dAction::handleAction()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
Unused Code introduced by
$result is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
156
        $this->assertEquals(200, Controller::curr()->getResponse()->getStatusCode());
157
        $this->assertEquals(
158
            'Comment approved.',
159
            Controller::curr()->getResponse()->getStatusDescription()
160
        );
161
162
        $record = DataObject::get_by_id(Comment::class, $recordID);
163
        $this->assertEquals(1, $record->Moderated);
164
        $this->assertEquals(0, $record->IsSpam);
165
    }
166
}
167