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

DMSTest::testFolderExcludedFromFilesystemSync()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 0
1
<?php
2
class DMSTest extends FunctionalTest
0 ignored issues
show
Coding Style Compatibility introduced by helpfulrobot
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...
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();
0 ignored issues
show
Documentation Bug introduced by Robbie Averill
It seems like \DMS::inst() of type object<DMSInterface> is incompatible with the declared type object<DMSInterace> of property $dms.

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...
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");
0 ignored issues
show
Bug introduced by helpfulrobot
The method assertNotNull() does not seem to exist on object<DMSTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
56
        $this->assertTrue(
0 ignored issues
show
Bug introduced by Robbie Averill
The method assertTrue() does not seem to exist on object<DMSTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
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");
0 ignored issues
show
Bug introduced by helpfulrobot
The method assertNotNull() does not seem to exist on object<DMSTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
74
            $this->assertTrue(file_exists($document->getFullPath()));
0 ignored issues
show
Bug introduced by helpfulrobot
The method assertTrue() does not seem to exist on object<DMSTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
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(
0 ignored issues
show
Bug introduced by Robbie Averill
The method assertTrue() does not seem to exist on object<DMSTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
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(
0 ignored issues
show
Bug introduced by Robbie Averill
The method assertTrue() does not seem to exist on object<DMSTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
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");
0 ignored issues
show
Bug introduced by helpfulrobot
The method assertNotNull() does not seem to exist on object<DMSTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
110
        $this->assertTrue(
0 ignored issues
show
Bug introduced by Robbie Averill
The method assertTrue() does not seem to exist on object<DMSTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
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");
0 ignored issues
show
Bug introduced by helpfulrobot
The method assertEquals() does not seem to exist on object<DMSTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
123
        $this->assertEquals("My custom description", $document->Description, "Custom description not modified");
0 ignored issues
show
Bug introduced by helpfulrobot
The method assertEquals() does not seem to exist on object<DMSTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
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
Duplication introduced by Robbie Averill
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');
0 ignored issues
show
Bug introduced by Robbie Averill
The method assertCount() does not seem to exist on object<DMSTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
135
        $this->assertContainsOnlyInstancesOf('DMSDocument', $documents);
0 ignored issues
show
Bug introduced by Robbie Averill
The method assertContainsOnlyInstancesOf() does not exist on DMSTest. Did you maybe mean assertContains()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
136
    }
137
138
    /**
139
     * Test that embargoed documents are excluded from getByPage
140
     */
141 View Code Duplication
    public function testGetByPageWithEmbargoedDocuments()
0 ignored issues
show
Duplication introduced by Robbie Averill
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');
0 ignored issues
show
Bug introduced by Robbie Averill
The method assertCount() does not seem to exist on object<DMSTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
147
        $this->assertContainsOnlyInstancesOf('DMSDocument', $documents);
0 ignored issues
show
Bug introduced by Robbie Averill
The method assertContainsOnlyInstancesOf() does not exist on DMSTest. Did you maybe mean assertContains()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
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());
0 ignored issues
show
Bug introduced by Robbie Averill
The method assertSame() does not seem to exist on object<DMSTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
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);
0 ignored issues
show
Bug introduced by Robbie Averill
The method assertCount() does not seem to exist on object<DMSTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
167
        $this->assertContainsOnlyInstancesOf('DMSDocumentSet', $sets);
0 ignored issues
show
Bug introduced by Robbie Averill
The method assertContainsOnlyInstancesOf() does not exist on DMSTest. Did you maybe mean assertContains()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
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