Completed
Pull Request — master (#27)
by
unknown
03:13
created

ExternalLinksTest   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 149
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 10

Importance

Changes 3
Bugs 1 Features 1
Metric Value
wmc 9
c 3
b 1
f 1
lcom 1
cbo 10
dl 0
loc 149
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A setUpOnce() 0 7 2
A setUp() 0 53 2
A testLinks() 0 58 2
1
<?php
2
3
namespace SilverStripe\ExternalLinks\Tests;
4
5
use SilverStripe\ExternalLinks\Tasks\LinkChecker;
6
use SilverStripe\Core\Injector\Injector;
7
use SilverStripe\ExternalLinks\Tasks\CheckExternalLinksTask;
8
use SilverStripe\ExternalLinks\Model\BrokenExternalPageTrackStatus;
9
use SilverStripe\i18n\i18n;
10
use SilverStripe\Reports\Report;
11
use SilverStripe\ExternalLinks\Reports\BrokenExternalLinksReport;
12
use SilverStripe\Dev\SapphireTest;
13
use SilverStripe\ExternalLinks\Tests\ExternalLinksTestPage;
14
use Phockito;
15
16
class ExternalLinksTest extends SapphireTest
17
{
18
19
    protected static $fixture_file = 'ExternalLinksTest.yml';
20
21
    protected static $extra_dataobjects = array(
22
        ExternalLinksTestPage::class
23
    );
24
25
    public function setUpOnce()
26
    {
27
        if (class_exists(Phockito::class)) {
28
            Phockito::include_hamcrest(false);
29
        }
30
31
        parent::setUpOnce();
0 ignored issues
show
Bug introduced by
The method setUpOnce() does not exist on SilverStripe\Dev\SapphireTest. Did you maybe mean setUp()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

31
        parent::/** @scrutinizer ignore-call */ 
32
                setUpOnce();

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
    }
33
34
    public function setUp()
35
    {
36
        parent::setUp();
37
38
        // Check dependencies
39
        if (!class_exists(Phockito::class)) {
40
            $this->skipTest = true;
0 ignored issues
show
Bug Best Practice introduced by
The property skipTest does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
41
            return $this->markTestSkipped("These tests need the Phockito module installed to run");
42
        }
43
44
        // Mock link checker
45
        $checker = Phockito::mock(LinkChecker::class);
46
        Phockito::when($checker)
47
            ->checkLink('http://www.working.com')
48
            ->return(200);
49
50
        Phockito::when($checker)
51
            ->checkLink('http://www.broken.com/url/thing') // 404 on working site
52
            ->return(404);
53
54
        Phockito::when($checker)
55
            ->checkLink('http://www.broken.com') // 403 on working site
56
            ->return(403);
57
58
        Phockito::when($checker)
59
            ->checkLink('http://www.nodomain.com') // no ping
60
            ->return(0);
61
62
        Phockito::when($checker)
63
            ->checkLink('/internal/link')
64
            ->return(null);
65
66
        Phockito::when($checker)
67
            ->checkLink('[sitetree_link,id=9999]')
68
            ->return(null);
69
70
        Phockito::when($checker)
71
            ->checkLink('home')
72
            ->return(null);
73
74
        Phockito::when($checker)
75
            ->checkLink('broken-internal')
76
            ->return(null);
77
78
        Phockito::when($checker)
79
            ->checkLink('[sitetree_link,id=1]')
80
            ->return(null);
81
82
        Phockito::when($checker)
83
            ->checkLink(Hamcrest_Matchers::anything()) // anything else is 404
0 ignored issues
show
Bug introduced by
The type SilverStripe\ExternalLinks\Tests\Hamcrest_Matchers was not found. Did you mean Hamcrest_Matchers? If so, make sure to prefix the type with \.
Loading history...
84
            ->return(404);
85
86
        Injector::inst()->registerService($checker, LinkChecker::class);
87
    }
88
89
    public function testLinks()
90
    {
91
        // Run link checker
92
        $task = CheckExternalLinksTask::create();
93
        $task->setSilent(true); // Be quiet during the test!
94
        $task->runLinksCheck();
95
96
        // Get all links checked
97
        $status = BrokenExternalPageTrackStatus::get_latest();
98
        $this->assertEquals('Completed', $status->Status);
99
        $this->assertEquals(5, $status->TotalPages);
100
        $this->assertEquals(5, $status->CompletedPages);
101
102
        // Check all pages have had the correct HTML adjusted
103
        for ($i = 1; $i <= 5; $i++) {
104
            $page = $this->objFromFixture('ExternalLinksTestPage', 'page'.$i);
105
            $this->assertNotEmpty($page->Content);
106
            $this->assertEquals(
107
                $page->ExpectedContent,
108
                $page->Content,
109
                "Assert that the content of page{$i} has been updated"
110
            );
111
        }
112
113
        // Check that the correct report of broken links is generated
114
        $links = $status
115
            ->BrokenLinks()
116
            ->sort('Link');
117
118
        $this->assertEquals(4, $links->count());
119
        $this->assertEquals(
120
            array(
121
                'http://www.broken.com',
122
                'http://www.broken.com/url/thing',
123
                'http://www.broken.com/url/thing',
124
                'http://www.nodomain.com'
125
            ),
126
            array_values($links->map('ID', 'Link')->toArray())
127
        );
128
129
        // Check response codes are correct
130
        $expected = array(
131
            'http://www.broken.com' => 403,
132
            'http://www.broken.com/url/thing' => 404,
133
            'http://www.nodomain.com' => 0
134
        );
135
        $actual = $links->map('Link', 'HTTPCode')->toArray();
136
        $this->assertEquals($expected, $actual);
137
138
        // Check response descriptions are correct
139
        i18n::set_locale('en_NZ');
140
        $expected = array(
141
            'http://www.broken.com' => '403 (Forbidden)',
142
            'http://www.broken.com/url/thing' => '404 (Not Found)',
143
            'http://www.nodomain.com' => '0 (Server Not Available)'
144
        );
145
        $actual = $links->map('Link', 'HTTPCodeDescription')->toArray();
146
        $this->assertEquals($expected, $actual);
147
    }
148
149
    /**
150
     * Test that broken links appears in the reports list
151
     */
152
    public function testReportExists()
153
    {
154
        $reports = Report::get_reports();
155
        $reportNames = array();
156
        foreach ($reports as $report) {
157
            $reportNames[] = $report->class;
158
        }
159
        $this->assertContains(
160
            BrokenExternalLinksReport::class,
161
            $reportNames,
162
            'BrokenExternalLinksReport is in reports list'
163
        );
164
    }
165
}
166