Completed
Push — master ( 07fcd3...4fbd85 )
by Robbie
9s
created

GridFieldClearAllButtonTest::getSubject()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace SilverLeague\LogViewer\Tests\Forms\GridField;
4
5
use SilverLeague\LogViewer\Forms\GridField\GridFieldClearAllButton;
6
use SilverLeague\LogViewer\Model\LogEntry;
7
use SilverStripe\Control\Controller;
8
use SilverStripe\Core\Injector\Injector;
9
use SilverStripe\Dev\SapphireTest;
10
use SilverStripe\Forms\FieldList;
11
use SilverStripe\Forms\Form;
12
use SilverStripe\Forms\GridField\GridField;
13
use SilverStripe\Forms\GridField\GridFieldConfig;
14
use SilverStripe\ORM\DataList;
15
16
/**
17
 * Tests for the "clear all" GridField action class
18
 *
19
 * @coversDefaultClass \SilverLeague\LogViewer\Forms\GridField\GridFieldClearAllButton
20
 * @package silverstripe-logviewer
21
 * @author  Robbie Averill <[email protected]>
22
 */
23
class GridFieldClearAllButtonTest extends SapphireTest
24
{
25
    /**
26
     * {@inheritDoc}
27
     */
28
    protected $usesDatabase = true;
29
30
    protected $gridField;
31
32
    /**
33
     * {@inheritDoc}
34
     */
35
    public function setUp()
36
    {
37
        parent::setUp();
38
39
        $config = GridFieldConfig::create()->addComponent(new GridFieldClearAllButton('before'));
40
        $this->gridField = GridField::create('logs', 'logs', DataList::create(LogEntry::class), $config);
41
        $form = Form::create(
0 ignored issues
show
Unused Code introduced by
$form 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...
42
            Controller::create(),
43
            'foobar',
44
            FieldList::create([$this->gridField]),
45
            FieldList::create()
46
        );
47
    }
48
49
    /**
50
     * Return the actual class we're testing
51
     *
52
     * @return GridFieldClearAllButton
53
     */
54
    protected function getSubject()
55
    {
56
        return $this->gridField->getConfig()->getComponentByType(GridFieldClearAllButton::class);
57
    }
58
59
    /**
60
     * Ensure that the HTML fragment was pushed correctly and assigned to the specified fragment (in setUp above)
61
     */
62
    public function testGetHtmlFragments()
63
    {
64
        $fragments = $this->getSubject()->getHTMLFragments($this->gridField);
65
66
        $this->assertArrayHasKey('before', $fragments);
67
        $this->assertContains('Clear all', $fragments['before']);
68
        $this->assertContains('clear-all-logs', $fragments['before']);
69
        $this->assertContains('font-icon-trash-bin action_clear', $fragments['before']);
70
        $this->assertContains('<p class="grid-clear-all-button">', $fragments['before']);
71
    }
72
73
    /**
74
     * Test that the GridFieldAction actions are returned correctly
75
     */
76
    public function testActionsAreDefined()
77
    {
78
        $this->assertSame(['clear'], (new GridFieldClearAllButton)->getActions($this->gridField));
79
    }
80
81
    /**
82
     * Test that an exception is thrown if the Member doesn't have permission to delete the data class assigned
83
     *
84
     * @expectedException \SilverStripe\ORM\ValidationException
85
     * @expectedExceptionMessage No permission to unlink record
86
     */
87
    public function testCannotClearAllWithoutPermission()
88
    {
89
        $forbiddenList = DataList::create(Stub\SomeDataObject::class);
90
        $this->gridField->setList($forbiddenList);
91
92
        $this->getSubject()->handleAction($this->gridField, 'clear', null, []);
0 ignored issues
show
Documentation introduced by
null is of type null, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
93
    }
94
95
    /**
96
     * Test that with permission the list can be cleared
97
     */
98
    public function testClearList()
99
    {
100
        $this->logInWithPermission('ADMIN');
101
102
        $this->createDummyLogs();
103
        $this->assertSame(5, $this->gridField->getList()->count());
104
105
        $this->getSubject()->handleAction($this->gridField, 'clear', null, []);
0 ignored issues
show
Documentation introduced by
null is of type null, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
106
        $this->assertSame(0, $this->gridField->getList()->count());
107
    }
108
109
    /**
110
     * Create a set of dummy LogEntry records
111
     *
112
     * @param int $limit
113
     */
114
    protected function createDummyLogs($limit = 5)
115
    {
116
        $factory = Injector::inst()->create('FixtureFactory');
117
118
        for ($i = 1; $i <= $limit; $i++) {
119
            $factory->createObject(
120
                LogEntry::class,
121
                'stub_log_' . $i,
122
                [
123
                    'Entry' => 'Log #' . $i,
124
                    'Level' => 'DEBUG'
125
                ]
126
            );
127
        }
128
    }
129
}
130