Completed
Push — master ( f9f56b...97d556 )
by Robbie
02:07
created

tests/DMSTest.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
class DMSTest extends FunctionalTest
3
{
4
    protected static $fixture_file = 'dmstest.yml';
5
6
    /**
7
     * Stub PDF files for testing
8
     *
9
     * @var string
10
     */
11
    public static $testFile = 'dms/tests/DMS-test-lorum-file.pdf';
12
    public static $testFile2 = 'dms/tests/DMS-test-document-2.pdf';
13
14
    /**
15
     * The test folder to write assets into
16
     *
17
     * @var string
18
     */
19
    protected $testDmsPath = 'assets/_dms-assets-test-1234';
20
21
    /**
22
     * @var DMSInterace
23
     */
24
    protected $dms;
25
26
    /**
27
     * Use a test DMS folder, so we don't overwrite the live one, and clear it out in case of previous broken tests
28
     *
29
     * {@inheritDoc}
30
     */
31
    public function setUp()
32
    {
33
        parent::setUp();
34
        Config::inst()->update('DMS', 'folder_name', $this->testDmsPath);
35
        DMSFilesystemTestHelper::delete($this->testDmsPath);
36
        $this->dms = DMS::inst();
37
    }
38
39
    /**
40
     * Delete the test folder after the test runs
41
     *
42
     * {@inheritDoc}
43
     */
44
    public function tearDown()
45
    {
46
        parent::tearDown();
47
        DMSFilesystemTestHelper::delete($this->testDmsPath);
48
    }
49
50
    public function testDMSStorage()
51
    {
52
        $file = self::$testFile;
53
        $document = $this->dms->storeDocument($file);
54
55
        $this->assertNotNull($document, "Document object created");
56
        $this->assertTrue(
57
            file_exists(
58
                DMS::inst()->getStoragePath() . DIRECTORY_SEPARATOR . $document->Folder
59
                . DIRECTORY_SEPARATOR . $document->Filename
60
            ),
61
            "Document file copied into DMS folder"
62
        );
63
    }
64
65
    public function testDMSFolderSpanning()
66
    {
67
        Config::inst()->update('DMS', 'folder_size', 5);
68
        $file = self::$testFile;
69
70
        $documents = array();
71
        for ($i = 0; $i <= 16; $i++) {
72
            $document = $this->dms->storeDocument($file);
73
            $this->assertNotNull($document, "Document object created on run number: $i");
74
            $this->assertTrue(file_exists($document->getFullPath()));
75
            $documents[] = $document;
76
        }
77
78
        // Test document objects have their folders set
79
        $folders = array();
80
        for ($i = 0; $i <= 16; $i++) {
81
            $folderName = $documents[$i]->Folder;
82
            $this->assertTrue(
83
                strpos($documents[$i]->getFullPath(), DIRECTORY_SEPARATOR . $folderName . DIRECTORY_SEPARATOR) !== false,
84
                "Correct folder name for the documents. Document path contains reference to folder name '$folderName'"
85
            );
86
            $folders[] = $folderName;
87
        }
88
89
        // Test we created 4 folder to contain the 17 files
90
        foreach ($folders as $f) {
91
            $this->assertTrue(
92
                is_dir(DMS::inst()->getStoragePath() . DIRECTORY_SEPARATOR . $f),
93
                "Document folder '$f' exists"
94
            );
95
        }
96
    }
97
98
    public function testReplaceDocument()
99
    {
100
        // Store the first document
101
        $document = $this->dms->storeDocument(self::$testFile);
102
        $document->Title = "My custom title";
103
        $document->Description = "My custom description";
104
        $document->write();
105
106
        // Then overwrite with a second document
107
        $document = $document->replaceDocument(self::$testFile2);
108
109
        $this->assertNotNull($document, "Document object created");
110
        $this->assertTrue(
111
            file_exists(
112
                DMS::inst()->getStoragePath() . DIRECTORY_SEPARATOR . $document->Folder
113
                . DIRECTORY_SEPARATOR . $document->Filename
114
            ),
115
            "Document file copied into DMS folder"
116
        );
117
        $this->assertContains(
118
            "DMS-test-document-2",
119
            $document->Filename,
120
            "Original document filename is contain in the new filename"
121
        );
122
        $this->assertEquals("My custom title", $document->Title, "Custom title not modified");
123
        $this->assertEquals("My custom description", $document->Description, "Custom description not modified");
124
    }
125
126
    /**
127
     * Test that documents can be returned by a given page
128
     */
129 View Code Duplication
    public function testGetByPageWithoutEmbargoes()
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...
130
    {
131
        $pageWithEmbargoes = $this->objFromFixture('SiteTree', 's3');
132
        $documents = $this->dms->getByPage($pageWithEmbargoes);
133
        // Fixture: 6 documents in set, 1 is embargoed
134
        $this->assertCount(5, $documents, 'Embargoed documents are excluded by default');
135
        $this->assertContainsOnlyInstancesOf('DMSDocument', $documents);
136
    }
137
138
    /**
139
     * Test that embargoed documents are excluded from getByPage
140
     */
141 View Code Duplication
    public function testGetByPageWithEmbargoedDocuments()
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...
142
    {
143
        $pageWithEmbargoes = $this->objFromFixture('SiteTree', 's3');
144
        $documents = $this->dms->getByPage($pageWithEmbargoes, true);
145
        // Fixture: 6 documents in set, 1 is embargoed
146
        $this->assertCount(6, $documents, 'Embargoed documents can be included');
147
        $this->assertContainsOnlyInstancesOf('DMSDocument', $documents);
148
    }
149
150
    /**
151
     * Ensure the shortcode handler key is configurable
152
     */
153
    public function testShortcodeHandlerKeyIsConfigurable()
154
    {
155
        Config::inst()->update('DMS', 'shortcode_handler_key', 'testing');
156
        $this->assertSame('testing', DMS::inst()->getShortcodeHandlerKey());
157
    }
158
159
    /**
160
     * Test that document sets can be retrieved for a given page
161
     */
162
    public function testGetDocumentSetsByPage()
163
    {
164
        $page = $this->objFromFixture('SiteTree', 's1');
165
        $sets = $this->dms->getDocumentSetsByPage($page);
166
        $this->assertCount(2, $sets);
167
        $this->assertContainsOnlyInstancesOf('DMSDocumentSet', $sets);
168
    }
169
170
    /**
171
     * Ensure that assets/* folders are not included in filesystem sync operations
172
     */
173
    public function testFolderExcludedFromFilesystemSync()
174
    {
175
        // Undo setup config changes
176
        Config::unnest();
177
        Config::nest();
178
179
        $result = Filesystem::config()->get('sync_blacklisted_patterns');
180
        $folderName = substr(DMS::config()->get('folder_name'), 7);
181
        $this->assertContains('/^' . $folderName . '$/i', $result);
182
    }
183
}
184