Skip to content

Commit 7ce09f8

Browse files
committed
Moved multiple h1 test cases to its own file + small bug fix
1 parent d5a1d6c commit 7ce09f8

File tree

3 files changed

+115
-92
lines changed

3 files changed

+115
-92
lines changed

src/HeadingStructure/Checker.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,6 @@ public function __construct(int $heading_shift = 1, bool $is_strict = false, boo
8282
}
8383
$this->is_strict = $is_strict;
8484
$this->allow_multiple_h1 = $allow_multiple_h1;
85-
$this->h1_count = 0;
8685
}
8786

8887
/**
@@ -105,6 +104,7 @@ public function evaluate(\DOMDocument $dom): array
105104
$this->errors = [];
106105
$this->heading_level_structure = [$this->heading_shift => 0];
107106
$this->heading_order = array_slice([1,2,3,4,5,6], 0, $this->heading_shift);
107+
$this->h1_count = 0;
108108

109109
if (count($dom->getElementsByTagName('body')) > 0) {
110110
$body = $dom->getElementsByTagName('body')[0];

tests/HeadingStructure/CheckerTest.php

Lines changed: 11 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -229,99 +229,19 @@ public function testHeadingShift(): void
229229

230230
public function testingMultipleH1(): void
231231
{
232-
// by default multiple h1 should fail
233-
$checker = new Checker(0, true);
234-
$html = "
235-
<h1>First h1</h1>
236-
<h1>Second h1</h1>
237-
";
238-
$testcase = new \testcase($html, [
239-
'passed' => false,
240-
'errors' => [
241-
(object) [
242-
'type' => 'heading unallowed',
243-
'tag' => 'h1',
244-
'text' => 'Second h1',
245-
'html' => '<h1>Second h1</h1>',
246-
'recommendation' => "Check and use only allowed headings (<h2>, <h3>, <h4>, <h5>, <h6>; multiple <h1> unallowed)."
247-
]
248-
]
249-
]);
250-
$dom = $this->getDOM($testcase->input);
251-
$this->assertEquals(
252-
$testcase->expected_output,
253-
$checker->evaluate($dom),
254-
print_r($testcase->input, true) . 'did not pass all checks.'
255-
);
256-
257-
// unallowed heading takes precedence than nesting problem
258-
$checker = new Checker(0, true);
259-
$html = "
260-
<div><h1>First h1</h1></div>
261-
<h1>Second h1</h1>
262-
";
263-
$testcase = new \testcase($html, [
264-
'passed' => false,
265-
'errors' => [
266-
(object) [
267-
'type' => 'heading unallowed',
268-
'tag' => 'h1',
269-
'text' => 'Second h1',
270-
'html' => '<h1>Second h1</h1>',
271-
'recommendation' => "Check and use only allowed headings (<h2>, <h3>, <h4>, <h5>, <h6>; multiple <h1> unallowed)."
272-
]
273-
]
274-
]);
275-
$dom = $this->getDOM($testcase->input);
276-
$this->assertEquals(
277-
$testcase->expected_output,
278-
$checker->evaluate($dom),
279-
print_r($testcase->input, true) . 'did not pass all checks.'
280-
);
232+
require "CheckerTestcases/testcases_multiple_h1.php";
281233

282-
// throw some other headings into the mix
283-
$checker = new Checker(0, false);
284-
$html = "
285-
<h1>First h1</h1>
286-
<h2>Some h2</h2>
287-
<h3>Some h3</h3>
288-
<h1>Second h1</h1>
289-
";
290-
$testcase = new \testcase($html, [
291-
'passed' => false,
292-
'errors' => [
293-
(object) [
294-
'type' => 'heading unallowed',
295-
'tag' => 'h1',
296-
'text' => 'Second h1',
297-
'html' => '<h1>Second h1</h1>',
298-
'recommendation' => "Check and use only allowed headings (<h2>, <h3>, <h4>, <h5>, <h6>; multiple <h1> unallowed)."
299-
]
300-
]
301-
]);
302-
$dom = $this->getDOM($testcase->input);
303-
$this->assertEquals(
304-
$testcase->expected_output,
305-
$checker->evaluate($dom),
306-
print_r($testcase->input, true) . 'did not pass all checks.'
307-
);
234+
$checker = new Checker(0, false, false);
235+
foreach ($testcases_multiple_h1_fail as $testcase) {
236+
$dom = $this->getDOM($testcase->input);
237+
$this->assertEquals($testcase->expected_output, $checker->evaluate($dom), "failed: ".$testcase->input);
238+
}
308239

309-
// if allow_multiple_h1 is set to true, multiple h1 should pass
310-
$checker = new Checker(0, true, true);
311-
$html = "
312-
<h1>First h1</h1>
313-
<h1>Second h1</h1>
314-
";
315-
$testcase = new \testcase($html, [
316-
'passed' => true,
317-
'errors' => []
318-
]);
319-
$dom = $this->getDOM($testcase->input);
320-
$this->assertEquals(
321-
$testcase->expected_output,
322-
$checker->evaluate($dom),
323-
print_r($testcase->input, true) . 'did not pass all checks.'
324-
);
240+
$checker = new Checker(0, false, true);
241+
foreach ($testcases_multiple_h1_pass as $testcase) {
242+
$dom = $this->getDOM($testcase->input);
243+
$this->assertEquals($testcase->expected_output, $checker->evaluate($dom));
244+
}
325245
}
326246

327247
private function getDOM($s)
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
<?php declare(strict_types=1);
2+
3+
/**
4+
* Test cases for Multiple H1
5+
*/
6+
7+
$testcases_multiple_h1_fail = array(
8+
//------------------------------------------------------------------------------
9+
// by default multiple h1 should fail
10+
new testcase(
11+
'
12+
<h1>First h1</h1>
13+
<h1>Second h1</h1>
14+
',
15+
array('passed'=>false,'errors'=>array(
16+
(object) [
17+
'type' => 'heading unallowed',
18+
'tag' => 'h1',
19+
'text' => 'Second h1',
20+
'html' => '<h1>Second h1</h1>',
21+
'recommendation' => "Check and use only allowed headings (<h2>, <h3>, <h4>, <h5>, <h6>; multiple <h1> unallowed)."
22+
]
23+
))
24+
),
25+
//------------------------------------------------------------------------------
26+
// unallowed heading takes precedence than nesting problem
27+
new testcase(
28+
'
29+
<h1>First h1</h1>
30+
<div><h1>Second h1</h1></div>
31+
',
32+
array('passed'=>false,'errors'=>array(
33+
(object) [
34+
'type' => 'heading unallowed',
35+
'tag' => 'h1',
36+
'text' => 'Second h1',
37+
'html' => '<h1>Second h1</h1>',
38+
'recommendation' => "Check and use only allowed headings (<h2>, <h3>, <h4>, <h5>, <h6>; multiple <h1> unallowed)."
39+
]
40+
))
41+
),
42+
//------------------------------------------------------------------------------
43+
// unallowed heading takes precedence than nesting problem (reversed)
44+
new testcase(
45+
'
46+
<div><h1>First h1</h1></div>
47+
<h1>Second h1</h1>
48+
',
49+
array('passed'=>false,'errors'=>array(
50+
(object) [
51+
'type' => 'heading unallowed',
52+
'tag' => 'h1',
53+
'text' => 'Second h1',
54+
'html' => '<h1>Second h1</h1>',
55+
'recommendation' => "Check and use only allowed headings (<h2>, <h3>, <h4>, <h5>, <h6>; multiple <h1> unallowed)."
56+
]
57+
))
58+
),
59+
//------------------------------------------------------------------------------
60+
// throw some other headings into the mix
61+
new testcase(
62+
'
63+
<h1>First h1</h1>
64+
<h2>Some h2</h2>
65+
<h3>Some h3</h3>
66+
<h1>Second h1</h1>
67+
',
68+
array('passed'=>false,'errors'=>array(
69+
(object) [
70+
'type' => 'heading unallowed',
71+
'tag' => 'h1',
72+
'text' => 'Second h1',
73+
'html' => '<h1>Second h1</h1>',
74+
'recommendation' => "Check and use only allowed headings (<h2>, <h3>, <h4>, <h5>, <h6>; multiple <h1> unallowed)."
75+
]
76+
))
77+
),
78+
//------------------------------------------------------------------------------
79+
);
80+
81+
82+
// if allow_multiple_h1 is set to true, multiple h1 should pass
83+
84+
$testcases_multiple_h1_pass = array(
85+
//------------------------------------------------------------------------------
86+
new testcase(
87+
'
88+
<h1>First h1</h1>
89+
<h1>Second h1</h1>
90+
',
91+
array('passed'=>true,'errors'=>[])
92+
),
93+
//------------------------------------------------------------------------------
94+
new testcase(
95+
'
96+
<h1>First h1</h1>
97+
<h1>Second h1</h1>
98+
<h1>Third h1</h1>
99+
<h1>Fourth h1</h1>
100+
',
101+
array('passed'=>true,'errors'=>[])
102+
),
103+
);

0 commit comments

Comments
 (0)