Completed
Push — master ( 3f2ac7...f065be )
by Dylan
02:41
created

AutomatedLinkTest::createPage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 4

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 4
nc 1
nop 1
1
<?php
2
3
/**
4
 * Class AutomatedLinkTest
5
 * Run tests to check the Automated Link Object and it's functionality
6
 *
7
 * @method void assertTrue(boolean $test, string $message)
8
 */
9
class AutomatedLinkTest extends SapphireTest {
10
11
    public static $fixture_file = 'fixtures/AutomatedLinkTest.yml';
12
13
    /**
14
     * Test that links are included in the content field
15
     */
16
    public function testInsertion(){
17
        $this->objFromFixture('AutomatedLink','link');
18
        $page   = $this->createPage('<p>Checking if phrase is replaced</p>');
19
        $link   = $this->getLinkFromPage($page);
20
        $this->assertTrue($link->nodeValue === 'phrase', 'Link wasn\'t created');
21
    }
22
23
    /**
24
     * Test the individual properties of the automated link
25
     */
26
    public function testProperties(){
27
        $this->objFromFixture('AutomatedLink','link2');
28
29
        $page   = self::createPage('<p>Checking if properties is with correct props</p>');
30
        $link   = self::getLinkFromPage($page);
31
32
        $this->assertTrue( $link->nodeValue === 'properties', 'Phrase did not match' );
33
        $this->assertTrue( $link->getAttribute('title') === 'test title', 'Title did not match' );
34
        $this->assertTrue( $link->getAttribute('href') === '#test-anchor', 'Anchor did not match' );
35
        $this->assertTrue( $link->getAttribute('target') === '_blank', 'Target did not match' );
36
        $this->assertTrue( $link->getAttribute('rel') === 'nofollow', 'Rel did not match' );
37
    }
38
39
    /**
40
     * Test that the default properties match what we expect
41
     */
42
    public function testDefaultProperties(){
43
        $this->objFromFixture('AutomatedLink','link3');
44
45
        $page   = self::createPage('<p>Checking if default is correct</p>');
46
        $link   = self::getLinkFromPage($page);
47
        $auto   = AutomatedLink::get()->find('Phrase', 'default');
48
49
        $this->assertTrue( $link->nodeValue === 'default', 'Phrase did not match' );
50
        $this->assertTrue( $link->getAttribute('title') === '', 'Title did not match' );
51
        $this->assertTrue( $link->getAttribute('href') === '#', 'href did not match' );
52
        $this->assertTrue( $link->getAttribute('target') === '', 'Target did not match' );
53
        $this->assertTrue( $link->getAttribute('rel') === '', 'Rel did not match' );
54
        $this->assertTrue( (int) $link->getAttribute('data-id') === $auto->ID, 'data-id did not match' );
55
    }
56
57
    /**
58
     * Test that the default properties match what we expect
59
     */
60
    public function testLimitFilter(){
61
        $page   = self::createPage('<p>Checking if default is created only twice. default default</p>');
62
        $dom    = self::getPageDOM($page);
63
        $links  = $dom->getElementsByTagName('a');
64
        $this->assertTrue($links->length == 2, 'Was suppose to find 2 link. Found '.$links->length);
65
    }
66
67
    /**
68
     * Test that the default properties match what we expect
69
     */
70
    public function testDisableFilter(){
71
        GlobalAutoLinkSettings::$enabled = false;
72
        $page   = self::createPage('<p>Checking if default is created at all when automated links are disabled.</p>');
73
        $links  = self::getLinksFromPage($page);
74
        $this->assertTrue($links->length < 1, 'Was not suppose to find 0 links. Found '.$links->length);
75
        GlobalAutoLinkSettings::$enabled = true;
76
    }
77
78
    /**
79
     * Renders the passed $page and returns all links found if any
80
     *
81
     * @param SiteTree $page
82
     * @return DOMNameList
83
     */
84
    public static function getLinksFromPage(SiteTree $page){
85
        $dom = self::getPageDOM($page);
0 ignored issues
show
Documentation introduced by
$page is of type object<SiteTree>, but the function expects a object<Page>.

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...
86
        return $dom->getElementsByTagName('a');
87
    }
88
89
    /**
90
     * Create a DOMDocument out of the $page supplied and
91
     * check that it only has 1 link.
92
     * If it does return it, else fail the test
93
     *
94
     * @param SiteTree $page
95
     * @return DOMDocument
96
     */
97
    public static function getLinkFromPage(SiteTree $page){
98
        $links = self::getLinksFromPage($page);
99
        return ( $links->length != 1 ) ? new DOMElement() : $links->item(0);
100
    }
101
102
    /**
103
     * Create a dummy object for testing functionality on the SiteTree
104
     *
105
     * @param string|null $content
106
     * @return Page
107
     */
108
    public static function createPage($content=null){
109
        $page = Page::create(array( 'Content' => $content ));
110
        $page->write();
111
        return $page;
112
    }
113
114
    /**
115
     * Render the $page supplied into a DOMDocument object
116
     *
117
     * @param Page $page
118
     * @return DOMDocument
119
     */
120
    public static function getPageDOM(Page $page){
121
        $controller = Page_Controller::create($page);
122
        $controller->invokeWithExtensions('addAutomatedLinks');
123
        return AutomatedLink::constructDOMDocument($controller->Content);
124
    }
125
}
126
127
128