Completed
Push — master ( fb989d...03fe48 )
by Franco
02:26
created

DMSTest::testShortcodeHandlerKeyIsConfigurable()   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
class DMSTest extends FunctionalTest
0 ignored issues
show
Coding Style Compatibility introduced by
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
     * @var string
9
     */
10
    public static $testFile = 'dms/tests/DMS-test-lorum-file.pdf';
11
    public static $testFile2 = 'dms/tests/DMS-test-document-2.pdf';
12
13
    /**
14
     * Store values to reset back to after this test runs
15
     */
16
    public static $dmsFolderOld;
17
    public static $dmsFolderSizeOld;
18
19
    /**
20
     * @var DMS
21
     */
22
    protected $dms;
23
24
    public function setUp()
25
    {
26
        parent::setUp();
27
28
        self::$dmsFolderOld = DMS::$dmsFolder;
29
        self::$dmsFolderSizeOld = DMS::$dmsFolderSize;
30
31
        //use a test DMS folder, so we don't overwrite the live one
32
        DMS::$dmsFolder = 'dms-assets-test-1234';
33
34
        //clear out the test folder (in case a broken test doesn't delete it)
35
        $this->delete(BASE_PATH . DIRECTORY_SEPARATOR . 'dms-assets-test-1234');
36
37
        $this->dms = DMS::inst();
38
    }
39
40 View Code Duplication
    public function tearDown()
0 ignored issues
show
Duplication introduced by
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...
41
    {
42
        parent::tearDown();
43
44
        self::$is_running_test = true;
45
46
        $d = DataObject::get("DMSDocument");
47
        foreach ($d as $d1) {
48
            $d1->delete();
49
        }
50
        $t = DataObject::get("DMSTag");
51
        foreach ($t as $t1) {
52
            $t1->delete();
53
        }
54
55
        //delete the test folder after the test runs
56
        $this->delete(BASE_PATH . DIRECTORY_SEPARATOR . 'dms-assets-test-1234');
57
58
        //set the old DMS folder back again
59
        DMS::$dmsFolder = self::$dmsFolderOld;
60
        DMS::$dmsFolderSize = self::$dmsFolderSizeOld;
61
62
        self::$is_running_test = $this->originalIsRunningTest;
63
    }
64
65
    /**
66
     * Delete a file that was created during a unit test
67
     *
68
     * @param string $path
69
     */
70 View Code Duplication
    public function delete($path)
0 ignored issues
show
Duplication introduced by
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...
71
    {
72
        if (file_exists($path) || is_dir($path)) {
73
            $it = new RecursiveIteratorIterator(
74
                new RecursiveDirectoryIterator($path),
75
                RecursiveIteratorIterator::CHILD_FIRST
76
            );
77
            foreach ($it as $file) {
78
                if (in_array($file->getBasename(), array('.', '..'))) {
79
                    continue;
80
                } elseif ($file->isDir()) {
81
                    rmdir($file->getPathname());
82
                } elseif ($file->isFile() || $file->isLink()) {
83
                    unlink($file->getPathname());
84
                }
85
            }
86
            rmdir($path);
87
        }
88
    }
89
90
    public function testDMSStorage()
91
    {
92
        $file = self::$testFile;
93
        $document = $this->dms->storeDocument($file);
94
95
        $this->assertNotNull($document, "Document object created");
0 ignored issues
show
Bug introduced by
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...
96
        $this->assertTrue(
0 ignored issues
show
Bug introduced by
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...
97
            file_exists(
98
                DMS::get_dms_path() . DIRECTORY_SEPARATOR . $document->Folder
99
                . DIRECTORY_SEPARATOR . $document->Filename
100
            ),
101
            "Document file copied into DMS folder"
102
        );
103
    }
104
105
    public function testDMSFolderSpanning()
106
    {
107
        DMS::$dmsFolderSize = 5;
108
        $file = self::$testFile;
109
110
        $documents = array();
111
        for ($i = 0; $i <= 16; $i++) {
112
            $document = $this->dms->storeDocument($file);
113
            $this->assertNotNull($document, "Document object created on run number: $i");
0 ignored issues
show
Bug introduced by
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...
114
            $this->assertTrue(file_exists($document->getFullPath()));
0 ignored issues
show
Bug introduced by
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...
115
            $documents[] = $document;
116
        }
117
118
        // Test document objects have their folders set
119
        $folders = array();
120
        for ($i = 0; $i <= 16; $i++) {
121
            $folderName = $documents[$i]->Folder;
122
            $this->assertTrue(
0 ignored issues
show
Bug introduced by
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...
123
                strpos($documents[$i]->getFullPath(), DIRECTORY_SEPARATOR . $folderName . DIRECTORY_SEPARATOR) !== false,
124
                "Correct folder name for the documents. Document path contains reference to folder name '$folderName'"
125
            );
126
            $folders[] = $folderName;
127
        }
128
129
        // Test we created 4 folder to contain the 17 files
130
        foreach ($folders as $f) {
131
            $this->assertTrue(is_dir(DMS::get_dms_path() . DIRECTORY_SEPARATOR . $f), "Document folder '$f' exists");
0 ignored issues
show
Bug introduced by
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...
132
        }
133
    }
134
135
    public function testReplaceDocument()
136
    {
137
        // Store the first document
138
        $document = $this->dms->storeDocument(self::$testFile);
139
        $document->Title = "My custom title";
140
        $document->Description = "My custom description";
141
        $document->write();
142
143
        // Then overwrite with a second document
144
        $document = $document->replaceDocument(self::$testFile2);
145
146
        $this->assertNotNull($document, "Document object created");
0 ignored issues
show
Bug introduced by
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...
147
        $this->assertTrue(
0 ignored issues
show
Bug introduced by
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...
148
            file_exists(
149
                DMS::get_dms_path() . DIRECTORY_SEPARATOR . $document->Folder
150
                . DIRECTORY_SEPARATOR . $document->Filename
151
            ),
152
            "Document file copied into DMS folder"
153
        );
154
        $this->assertContains(
155
            "DMS-test-document-2",
156
            $document->Filename,
157
            "Original document filename is contain in the new filename"
158
        );
159
        $this->assertEquals("My custom title", $document->Title, "Custom title not modified");
0 ignored issues
show
Bug introduced by
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...
160
        $this->assertEquals("My custom description", $document->Description, "Custom description not modified");
0 ignored issues
show
Bug introduced by
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...
161
    }
162
163
    /**
164
     * Test that documents can be returned by a given page
165
     */
166 View Code Duplication
    public function testGetByPageWithoutEmbargoes()
0 ignored issues
show
Duplication introduced by
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...
167
    {
168
        $pageWithEmbargoes = $this->objFromFixture('SiteTree', 's3');
169
        $documents = $this->dms->getByPage($pageWithEmbargoes);
0 ignored issues
show
Documentation introduced by
$pageWithEmbargoes is of type object<DataObject>|null, but the function expects a object<SiteTree>.

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...
170
        // Fixture: 6 documents in set, 1 is embargoed
171
        $this->assertCount(5, $documents, 'Embargoed documents are excluded by default');
0 ignored issues
show
Bug introduced by
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...
172
        $this->assertContainsOnlyInstancesOf('DMSDocument', $documents);
0 ignored issues
show
Bug introduced by
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...
173
    }
174
175
    /**
176
     * Test that embargoed documents are excluded from getByPage
177
     */
178 View Code Duplication
    public function testGetByPageWithEmbargoedDocuments()
0 ignored issues
show
Duplication introduced by
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...
179
    {
180
        $pageWithEmbargoes = $this->objFromFixture('SiteTree', 's3');
181
        $documents = $this->dms->getByPage($pageWithEmbargoes, true);
0 ignored issues
show
Documentation introduced by
$pageWithEmbargoes is of type object<DataObject>|null, but the function expects a object<SiteTree>.

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...
182
        // Fixture: 6 documents in set, 1 is embargoed
183
        $this->assertCount(6, $documents, 'Embargoed documents can be included');
0 ignored issues
show
Bug introduced by
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...
184
        $this->assertContainsOnlyInstancesOf('DMSDocument', $documents);
0 ignored issues
show
Bug introduced by
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...
185
    }
186
187
    /**
188
     * Ensure the shortcode handler key is configurable
189
     */
190
    public function testShortcodeHandlerKeyIsConfigurable()
191
    {
192
        Config::inst()->update('DMS', 'shortcode_handler_key', 'testing');
193
        $this->assertSame('testing', DMS::inst()->getShortcodeHandlerKey());
0 ignored issues
show
Bug introduced by
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...
194
    }
195
196
    /**
197
     * Test that document sets can be retrieved for a given page
198
     */
199
    public function testGetDocumentSetsByPage()
200
    {
201
        $page = $this->objFromFixture('SiteTree', 's1');
202
        $sets = $this->dms->getDocumentSetsByPage($page);
0 ignored issues
show
Documentation introduced by
$page is of type object<DataObject>|null, but the function expects a object<SiteTree>.

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...
203
        $this->assertCount(2, $sets);
0 ignored issues
show
Bug introduced by
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...
204
        $this->assertContainsOnlyInstancesOf('DMSDocumentSet', $sets);
0 ignored issues
show
Bug introduced by
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...
205
    }
206
}
207