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

GlobalAutoLinkSettingsTest::testIncludeIn()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 32
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 32
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 21
nc 1
nop 0
1
<?php
2
3
/**
4
 * Class GlobalAutoLinkSettingsTest
5
 * Run tests to check the GlobalAutoLinkSettings Object and it's functionality
6
 *
7
 * @method void assertTrue(boolean $test, string $message)
8
 */
9
class GlobalAutoLinkSettingsTest extends SapphireTest {
10
11
    public static $fixture_file = 'fixtures/AutomatedLinkTest.yml';
12
13
    /**
14
     * Test Max Links per page
15
     */
16
    public function testMaxLinksPerPage(){
17
        $this->objFromFixture('AutomatedLink','link');
18
        $this->objFromFixture('AutomatedLink','link2');
19
        $this->objFromFixture('AutomatedLink','link3');
20
21
        $settings = GlobalAutoLinkSettings::get()->first();
22
        $settings->MaxLinksPerPage = 1;
23
        $settings->write();
24
25
        $page   = AutomatedLinkTest::createPage('<p>Checking if only 1 link is created in phrase, properties, default</p>');
26
        $links  = AutomatedLinkTest::getLinksFromPage($page);
27
        $this->assertTrue($links->length === 1, 'Was not suppose to find 1 link. Found '.$links->length);
28
    }
29
30
    /**
31
     * Test that links are not created inside of exclude tags
32
     */
33
    public function testExcludeTags(){
34
        $settings = GlobalAutoLinkSettings::get()->first();
35
        $settings->MaxLinksPerPage = 0;
36
        $settings->write();
37
38
        $page = AutomatedLinkTest::createPage(
39
            '<p>Checking if only 2 links is created in default & phrase</p><h3>not in properties</h3>'
40
        );
41
        $links = AutomatedLinkTest::getLinksFromPage($page);
42
        $this->assertTrue($links->length === 2, 'Was not suppose to find 2 links. Found '.$links->length);
43
    }
44
45
    /**
46
     * Test that links are included in all the specified fields
47
     */
48
    public function testIncludeIn(){
49
        $settings = GlobalAutoLinkSettings::get()->first();
50
        $settings->IncludeIn = 'Content, Introduction';
51
        $settings->write();
52
53
        $page = TestPage::create(array(
54
            'Introduction' => '<div id="introduction">2 links should be created here. properties, phrase</div>',
55
            'Content' => '<div id="content">1 link should be created here. phrase</div>',
56
        ));
57
58
        $controller = Page_Controller::create($page);
59
        $controller->invokeWithExtensions('addAutomatedLinks');
60
61
        $rendered = $controller->Introduction.$controller->Content;
62
        $dom = AutomatedLink::constructDOMDocument($rendered);
63
64
        // Introduction
65
        $xpath = new DOMXPath($dom);
66
        $intro = $xpath->query("//*[@id='introduction']")->item(0);
67
        $this->assertTrue(
68
            $intro->getElementsByTagName('a')->length === 2,
69
            'Was suppose to find 1 link in Introduction'
70
        );
71
72
        // Content
73
        $xpath = new DOMXPath($dom);
74
        $intro = $xpath->query("//*[@id='content']")->item(0);
75
        $this->assertTrue(
76
            $intro->getElementsByTagName('a')->length === 1,
77
            'Was suppose to find 1 link in Introduction'
78
        );
79
    }
80
}
81
82
/**
83
 * Simple test page to test settings
84
 */
85
class TestPage extends Page implements TestOnly{
1 ignored issue
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
86
    private static $db = array(
1 ignored issue
show
Unused Code introduced by
The property $db is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
87
        'Introduction' => 'HTMLText'
88
    );
89
}
90
91
92