Completed
Push — master ( 354786...9ea506 )
by Robbie
07:25
created

ExternalLinksTest::tearDown()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 4
rs 10
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
3
class ExternalLinksTest 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...
4
5
	protected static $fixture_file = 'ExternalLinksTest.yml';
6
7
	protected $extraDataObjects = array(
8
		'ExternalLinksTestPage'
9
	);
10
11
    protected $illegalExtensions = array(
12
        'SiteTree' => array('Translatable')
13
    );
14
15
	public function setUpOnce() {
16
		if (class_exists('Phockito')) {
17
			Phockito::include_hamcrest(false);
18
		}
19
20
		parent::setUpOnce();
21
	}
22
23
	public function setUp() {
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
24
		parent::setUp();
25
26
		// Check dependencies
27
		if (!class_exists('Phockito')) {
28
			$this->skipTest = true;
29
			return $this->markTestSkipped("These tests need the Phockito module installed to run");
0 ignored issues
show
Bug introduced by
The method markTestSkipped() does not seem to exist on object<ExternalLinksTest>.

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...
30
		}
31
32
		// Mock link checker
33
		$checker = Phockito::mock('LinkChecker');
34
		Phockito::when($checker)
35
			->checkLink('http://www.working.com')
36
			->return(200);
37
38
		Phockito::when($checker)
39
			->checkLink('http://www.broken.com/url/thing') // 404 on working site
40
			->return(404);
41
42
		Phockito::when($checker)
43
			->checkLink('http://www.broken.com') // 403 on working site
44
			->return(403);
45
46
		Phockito::when($checker)
47
			->checkLink('http://www.nodomain.com') // no ping
48
			->return(0);
49
50
		Phockito::when($checker)
51
			->checkLink('/internal/link')
52
			->return(null);
53
54
		Phockito::when($checker)
55
			->checkLink('[sitetree_link,id=9999]')
56
			->return(null);
57
58
		Phockito::when($checker)
59
			->checkLink('home')
60
			->return(null);
61
62
		Phockito::when($checker)
63
			->checkLink('broken-internal')
64
			->return(null);
65
66
		Phockito::when($checker)
67
			->checkLink('[sitetree_link,id=1]')
68
			->return(null);
69
70
		Phockito::when($checker)
71
			->checkLink(Hamcrest_Matchers::anything()) // anything else is 404
72
			->return(404);
73
74
		Injector::inst()->registerService($checker, 'LinkChecker');
75
	}
76
77
	public function testLinks() {
78
		// Run link checker
79
		$task = CheckExternalLinksTask::create();
80
		$task->setSilent(true); // Be quiet during the test!
81
		$task->runLinksCheck();
82
83
		// Get all links checked
84
		$status = BrokenExternalPageTrackStatus::get_latest();
85
		$this->assertEquals('Completed', $status->Status);
0 ignored issues
show
Documentation introduced by
The property Status does not exist on object<BrokenExternalPageTrackStatus>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
Bug introduced by
The method assertEquals() does not seem to exist on object<ExternalLinksTest>.

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...
86
		$this->assertEquals(5, $status->TotalPages);
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<ExternalLinksTest>.

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...
87
		$this->assertEquals(5, $status->CompletedPages);
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<ExternalLinksTest>.

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...
88
89
		// Check all pages have had the correct HTML adjusted
90
		for($i = 1; $i <= 5; $i++) {
91
			$page = $this->objFromFixture('ExternalLinksTestPage', 'page'.$i);
92
			$this->assertNotEmpty($page->Content);
0 ignored issues
show
Bug introduced by
The method assertNotEmpty() does not seem to exist on object<ExternalLinksTest>.

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...
93
			$this->assertEquals(
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<ExternalLinksTest>.

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...
94
				$page->ExpectedContent,
95
				$page->Content,
96
				"Assert that the content of page{$i} has been updated"
97
			);
98
		}
99
100
		// Check that the correct report of broken links is generated
101
		$links = $status
102
			->BrokenLinks()
103
			->sort('Link');
104
105
		$this->assertEquals(4, $links->count());
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<ExternalLinksTest>.

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...
106
		$this->assertEquals(
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<ExternalLinksTest>.

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...
107
			array(
108
				'http://www.broken.com',
109
				'http://www.broken.com/url/thing',
110
				'http://www.broken.com/url/thing',
111
				'http://www.nodomain.com'
112
			),
113
			array_values($links->map('ID', 'Link')->toArray())
114
		);
115
116
		// Check response codes are correct
117
		$expected = array(
118
			'http://www.broken.com' => 403,
119
			'http://www.broken.com/url/thing' => 404,
120
			'http://www.nodomain.com' => 0
121
		);
122
		$actual = $links->map('Link', 'HTTPCode')->toArray();
123
		$this->assertEquals($expected, $actual);
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<ExternalLinksTest>.

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...
124
125
		// Check response descriptions are correct
126
		i18n::set_locale('en_NZ');
127
		$expected = array(
128
			'http://www.broken.com' => '403 (Forbidden)',
129
			'http://www.broken.com/url/thing' => '404 (Not Found)',
130
			'http://www.nodomain.com' => '0 (Server Not Available)'
131
		);
132
		$actual = $links->map('Link', 'HTTPCodeDescription')->toArray();
133
		$this->assertEquals($expected, $actual);
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<ExternalLinksTest>.

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...
134
	}
135
136
	/**
137
	 * Test that broken links appears in the reports list
138
	 */
139
	public function testReportExists() {
140
		$reports = SS_Report::get_reports();
141
		$reportNames = array();
142
		foreach($reports as $report) {
143
			$reportNames[] = $report->class;
144
		}
145
		$this->assertContains('BrokenExternalLinksReport',$reportNames,
146
			'BrokenExternalLinksReport is in reports list');
147
	}
148
}
149