Completed
Push — master ( f9f56b...97d556 )
by Robbie
03:43 queued 02:06
created

tests/cms/DMSDocumentAddControllerTest.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
class DMSDocumentAddControllerTest extends FunctionalTest
4
{
5
    protected static $fixture_file = 'dms/tests/dmstest.yml';
6
7
    /**
8
     * @var DMSDocumentAddController
9
     */
10
    protected $controller;
11
12
    public function setUp()
13
    {
14
        parent::setUp();
15
        $this->logInWithPermission();
16
        $this->controller = new DMSDocumentAddController;
17
        $this->controller->init();
18
    }
19
20
    /**
21
     * Ensure that if no ID is provided then a SiteTree singleton is returned (which will not have an ID). If one is
22
     * provided then it should be loaded from the database via versioning.
23
     */
24 View Code Duplication
    public function testCurrentPageReturnsSiteTree()
0 ignored issues
show
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...
25
    {
26
        $page = $this->objFromFixture('SiteTree', 's1');
27
28
        $this->assertInstanceOf('SiteTree', $this->controller->currentPage());
29
        $this->assertEmpty($this->controller->currentPage()->ID);
30
        $this->controller->setRequest(new SS_HTTPRequest('GET', '/', array('page_id' => $page->ID)));
31
        $this->assertEquals($page->ID, $this->controller->currentPage()->ID, 'Specified page is loaded and returned');
32
    }
33
34
    /**
35
     * Ensure that if no "dsid" is given a singleton is returned (which will not have an ID). If one is provided
36
     * it should be loaded from the database
37
     */
38 View Code Duplication
    public function testGetCurrentDocumentSetReturnsDocumentSet()
0 ignored issues
show
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...
39
    {
40
        $set = $this->objFromFixture('DMSDocumentSet', 'ds1');
41
42
        $this->assertInstanceOf('DMSDocumentSet', $this->controller->getCurrentDocumentSet());
43
        $this->assertEmpty($this->controller->getCurrentDocumentSet()->ID, 'Singleton does not have an ID');
44
        $this->controller->setRequest(new SS_HTTPRequest('GET', '/', array('dsid' => $set->ID)));
45
        $this->assertEquals($set->ID, $this->controller->getCurrentDocumentSet()->ID, 'Specified document set is returned');
46
    }
47
48
    /**
49
     * Test that extra allowed extensions are merged into the default upload field allowed extensions
50
     */
51
    public function testGetAllowedExtensions()
52
    {
53
        Config::inst()->remove('File', 'allowed_extensions');
54
        Config::inst()->update('File', 'allowed_extensions', array('jpg', 'gif'));
55
        $this->assertSame(array('jpg', 'gif'), $this->controller->getAllowedExtensions());
56
57
        Config::inst()->update('DMSDocumentAddController', 'allowed_extensions', array('php', 'php5'));
58
        $this->assertSame(array('jpg', 'gif', 'php', 'php5'), $this->controller->getAllowedExtensions());
59
    }
60
61
    /**
62
     * Test that the back link will be the document set that a file is uploaded into if relevant, otherwise the model
63
     * admin that it was uploaded from
64
     */
65
    public function testBacklink()
66
    {
67
        // No page ID and no document set ID
68
        $this->assertContains('admin/documents', $this->controller->Backlink());
69
70
        // No page ID, has document set ID
71
        $request = new SS_HTTPRequest('GET', '/', array('dsid' => 123));
72
        $this->controller->setRequest($request);
73
        $this->assertContains('EditForm', $this->controller->Backlink());
74
        $this->assertContains('123', $this->controller->Backlink());
75
76
        // Has page ID and document set ID
77
        $request = new SS_HTTPRequest('GET', '/', array('dsid' => 123, 'page_id' => 234));
78
        $this->controller->setRequest($request);
79
        $this->assertContains('admin/pages', $this->controller->Backlink());
80
        $this->assertContains('123', $this->controller->Backlink());
81
    }
82
83
    /**
84
     * Test that the document autocomplete endpoint returns JSON, matching on ID, title or filename (case insensitive)
85
     */
86
    public function testDocumentAutocomplete()
87
    {
88
        $result = (string) $this->get('admin/pages/adddocument/documentautocomplete?term=EXIST')->getBody();
89
        $this->assertJson($result, 'Autocompleter should return JSON');
90
        $this->assertContains("File That Doesn't Exist (Title)", $result);
91
        $this->assertContains('test-file-file-doesnt-exist-1', $result);
92
        $this->assertNotContains('doc-logged-in-users', $result);
93
94
        $document = $this->objFromFixture('DMSDocument', 'd2');
95
        $result = (string) $this->get('admin/pages/adddocument/documentautocomplete?term=' . $document->ID)->getBody();
96
        $this->assertContains($document->ID . " - File That Doesn't Exist (Title)", $result);
97
    }
98
}
99