Completed
Push — develop ( c1aab7...17fbc1 )
by Dylan
02:43
created

AutomatedLinkTest   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 3
dl 0
loc 99
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A testInsertion() 0 6 1
A testProperties() 0 12 1
A testDefaultProperties() 0 14 1
A testLimitFilter() 0 8 1
A getLinkFromPage() 0 6 2
A createPage() 0 5 1
A getPageDOM() 0 5 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   = $this->createPage('<p>Checking if properties is with correct props</p>');
30
        $link   = $this->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   = $this->createPage('<p>Checking if default is correct</p>');
46
        $link   = $this->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
        $this->objFromFixture('AutomatedLink','link3');
62
63
        $page   = $this->createPage('<p>Checking if default is created only twice. default default</p>');
64
        $dom    = $this->getPageDOM($page);
65
        $links  = $dom->getElementsByTagName('a');
66
        $this->assertTrue($links->length == 2, 'Was suppose to find 2 link. Found '.$links->length);
67
    }
68
69
    /**
70
     * Create a DOMDocument out of the $page supplied and
71
     * check that it only has 1 link.
72
     * If it does return it, else fail the test
73
     *
74
     * @param SiteTree $page
75
     * @return DOMDocument
76
     */
77
    private function getLinkFromPage(SiteTree $page){
78
        $dom = $this->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...
79
        $links = $dom->getElementsByTagName('a');
80
        $this->assertTrue($links->length == 1, 'Didn\'t find the link that was suppose to be created');
81
        return ( $links->length != 1 ) ? new DOMElement() : $links->item(0);
82
    }
83
84
    /**
85
     * Create a dummy object for testing functionality on the SiteTree
86
     *
87
     * @param string|null $content
88
     * @return Page
89
     */
90
    private function createPage($content=null){
91
        $page = Page::create(array( 'Content' => $content ));
92
        $page->write();
93
        return $page;
94
    }
95
96
    /**
97
     * Render the $page supplied into a DOMDocument object
98
     *
99
     * @param Page $page
100
     * @return DOMDocument
101
     */
102
    private function getPageDOM(Page $page){
103
        $controller = Page_Controller::create($page);
104
        $controller->invokeWithExtensions('addAutomatedLinks');
105
        return AutomatedLink::constructDOMDocument($controller->Content);
106
    }
107
}
108
109
110