Completed
Push — master ( d001a9...e5c7d1 )
by Robbie
27s queued 18s
created

DMSGridFieldAddNewButtonTest::getButtonHtml()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
3
class DMSGridFieldAddNewButtonTest extends SapphireTest
0 ignored issues
show
Coding Style Compatibility introduced by Sacha Judd
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...
4
{
5
    protected static $fixture_file = 'dms/tests/dmstest.yml';
6
7
    /**
8
     * @var DMSGridFieldAddNewButton
9
     */
10
    protected $button;
11
12
    /**
13
     * @var GridField
14
     */
15
    protected $gridField;
16
17
    public function setUp()
18
    {
19
        parent::setUp();
20
21
        $fakeList = DMSDocument::get();
22
        $this->gridField = GridField::create('TestGridField', false, $fakeList);
23
        $this->button = new DMSGridFieldAddNewButton;
24
    }
25
26
    /**
27
     * Test that when no document set ID is present then it is not added to the URL for "add document"
28
     */
29
    public function testNoDocumentSetIdInAddUrlWhenNotProvided()
30
    {
31
        $this->assertNotContains('?dsid', $this->getButtonHtml());
32
    }
33
34
    /**
35
     * Test that when a document set ID is provided, it is added onto the "add document" link
36
     */
37
    public function testDocumentSetIdAddedToLinkWhenProvided()
38
    {
39
        $this->button->setDocumentSetId(123);
40
        $this->assertContains('?dsid=123', $this->getButtonHtml());
41
    }
42
43
    /**
44
     * If a set is saved and associated to a page, that page's ID should be added to the "add document" link to help
45
     * to ensure the user gets redirected back to the correct place afterwards
46
     */
47
    public function testPageIdIsAddedWhenAvailableViaDocumentSetRelationship()
48
    {
49
        $set = $this->objFromFixture('DMSDocumentSet', 'ds1');
50
        $this->button->setDocumentSetId($set->ID);
51
52
        $controller = new ContentController;
53
        $controller->pushCurrent();
54
55
        $result = $this->getButtonHtml();
56
        $this->assertContains('dsid=' . $set->ID, $result, 'Add new button contains document set ID');
57
        $this->assertNotContains('page_id=' . $set->Page()->ID, $result, 'No page ID when not editing in page context');
58
59
        $controller = new CMSPageEditController;
60
        $controller->pushCurrent();
61
62
        $this->assertContains(
63
            'page_id=' . $set->Page()->ID,
64
            $this->getButtonHtml(),
65
            'Button contains page ID when in edit page context'
66
        );
67
    }
68
69
    /**
70
     * Returns the HTML result of the "add new" button, used for DRY in test class
71
     *
72
     * @return string
73
     */
74
    protected function getButtonHtml()
75
    {
76
        $fragments = $this->button->getHTMLFragments($this->gridField);
77
        return array_pop($fragments)->getValue();
78
    }
79
}
80