Completed
Pull Request — master (#163)
by Robbie
02:35
created

DMSShortcodeHandlerTest::testShortcodeOperation()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 24
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 24
rs 8.9713
c 0
b 0
f 0
cc 1
eloc 15
nc 1
nop 0
1
<?php
2
/**
3
 * Tests DMS shortcode linking functionality.
4
 *
5
 * @package dms
6
 * @subpackage tests
7
 */
8
class DMSShortcodeHandlerTest extends SapphireTest
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...
9
{
10
    protected static $fixture_file = 'dmstest.yml';
11
12
    public function testShortcodeOperation()
13
    {
14
        Config::inst()->update('DMS', 'folder_name', 'assets/_unit-test-123');
15
16
        $file = 'dms/tests/DMS-test-lorum-file.pdf';
17
        $document = DMS::inst()->storeDocument($file);
18
19
        $result = ShortcodeParser::get('default')->parse(sprintf(
20
            '<p><a href="[dms_document_link id=\'%d\']">Document</a></p>',
21
            $document->ID
22
        ));
23
24
        $value = Injector::inst()->create('HTMLValue', $result);
25
        $link = $value->query('//a')->item(0);
26
27
        $this->assertStringEndsWith(
0 ignored issues
show
Bug introduced by
The method assertStringEndsWith() does not seem to exist on object<DMSShortcodeHandlerTest>.

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...
28
            '/dmsdocument/' . $document->ID . '-dms-test-lorum-file-pdf',
29
            $link->getAttribute('href')
30
        );
31
        $this->assertEquals($document->getExtension(), $link->getAttribute('data-ext'));
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<DMSShortcodeHandlerTest>.

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...
32
        $this->assertEquals($document->getFileSizeFormatted(), $link->getAttribute('data-size'));
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<DMSShortcodeHandlerTest>.

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...
33
34
        DMSFilesystemTestHelper::delete('assets/_unit-test-123');
35
    }
36
37
    /**
38
     * When the document is valid, the correct arguments are provided and some content is given, the content should
39
     * be parsed and added into an anchor to the document
40
     */
41
    public function testShortcodeWithContentReturnsParsedContentInLink()
42
    {
43
        $document = $this->objFromFixture('DMSDocument', 'd1');
44
        $arguments = array('id' => $document->ID);
45
        $result = DMSShortcodeHandler::handle($arguments, 'Some content', ShortcodeParser::get('default'), '');
46
47
        $this->assertSame(
0 ignored issues
show
Bug introduced by
The method assertSame() does not seem to exist on object<DMSShortcodeHandlerTest>.

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...
48
            '<a href="/dmsdocument/' . $document->ID . '-test-file-file-doesnt-exist-1">Some content</a>',
49
            $result
50
        );
51
    }
52
53
    /**
54
     * An error page link should be returned if the arguments are not valid, empty or the document is not available etc.
55
     *
56
     * This only applies when an error page with a 404 error code exists.
57
     */
58
    public function testReturnErrorPageWhenIdIsEmpty()
59
    {
60
        ErrorPage::create(array('URLSegment' => 'testing', 'ErrorCode' => '404'))->write();
61
        $result = DMSShortcodeHandler::handle(array(), '', ShortcodeParser::get('default'), '');
62
        $this->assertContains('testing', $result);
63
    }
64
65
    /**
66
     * When invalid or no data is available to return from the arguments and no error page exists to use for a link,
67
     * return a blank string
68
     */
69
    public function testReturnEmptyStringWhenNoErrorPageExistsAndIdIsEmpty()
70
    {
71
        $this->assertSame('', DMSShortcodeHandler::handle(array(), '', ShortcodeParser::get('default'), ''));
0 ignored issues
show
Bug introduced by
The method assertSame() does not seem to exist on object<DMSShortcodeHandlerTest>.

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...
72
    }
73
}
74