|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace dlindberg\BlobChunk\Manager; |
|
6
|
|
|
|
|
7
|
|
|
use dlindberg\BlobChunk\Manager\Config as Config; |
|
8
|
|
|
use dlindberg\BlobChunk\Manager\Recursive as Recursive; |
|
9
|
|
|
use dlindberg\DOMDocumentFactory\DOMDocumentFactory; |
|
10
|
|
|
use dlindberg\DOMDocumentFactory\DOMDocumentFactoryConfig; |
|
11
|
|
|
|
|
12
|
|
|
final class RecursiveTest extends \PHPUnit\Framework\TestCase |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @var array |
|
16
|
|
|
*/ |
|
17
|
|
|
private $testA = [ |
|
18
|
|
|
['group' => 'recursive', 'parents' => ['tags' => ['ul', 'ol',]], 'children' => ['tags' => ['li',],],], |
|
19
|
|
|
]; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var array |
|
23
|
|
|
*/ |
|
24
|
|
|
private $testB = [ |
|
25
|
|
|
[ |
|
26
|
|
|
'group' => 'recursive', |
|
27
|
|
|
'parents' => [ |
|
28
|
|
|
'attributes' => [ |
|
29
|
|
|
['name' => 'id', 'value' => 'a'], |
|
30
|
|
|
['name' => 'class', 'value' => 'b'], |
|
31
|
|
|
], |
|
32
|
|
|
], |
|
33
|
|
|
'children' => [ |
|
34
|
|
|
'attributes' => [ |
|
35
|
|
|
['name' => 'id', 'value' => 'c'], |
|
36
|
|
|
['name' => 'class', 'value' => 'd'], |
|
37
|
|
|
], |
|
38
|
|
|
], |
|
39
|
|
|
], |
|
40
|
|
|
]; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @var array |
|
44
|
|
|
*/ |
|
45
|
|
|
private $testC = [ |
|
46
|
|
|
[ |
|
47
|
|
|
'group' => 'recursive', |
|
48
|
|
|
'parents' => [ |
|
49
|
|
|
'tags' => ['ul', 'ol'], |
|
50
|
|
|
'attributes' => [ |
|
51
|
|
|
['name' => 'id', 'value' => 'a'], |
|
52
|
|
|
['name' => 'class', 'value' => 'b'], |
|
53
|
|
|
], |
|
54
|
|
|
], |
|
55
|
|
|
'children' => [ |
|
56
|
|
|
'tags' => ['li',], |
|
57
|
|
|
'attributes' => [ |
|
58
|
|
|
['name' => 'id', 'value' => 'c'], |
|
59
|
|
|
['name' => 'class', 'value' => 'd'], |
|
60
|
|
|
], |
|
61
|
|
|
], |
|
62
|
|
|
], |
|
63
|
|
|
]; |
|
64
|
|
|
/** |
|
65
|
|
|
* @var Recursive |
|
66
|
|
|
*/ |
|
67
|
|
|
private $rA; |
|
68
|
|
|
/** |
|
69
|
|
|
* @var Recursive |
|
70
|
|
|
*/ |
|
71
|
|
|
private $rB; |
|
72
|
|
|
/** |
|
73
|
|
|
* @var Recursive |
|
74
|
|
|
*/ |
|
75
|
|
|
private $rC; |
|
76
|
|
|
/** |
|
77
|
|
|
* @var DOMDocumentFactory |
|
78
|
|
|
*/ |
|
79
|
|
|
private $docFactory; |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @var string[] |
|
83
|
|
|
*/ |
|
84
|
|
|
private $samples = [ |
|
85
|
|
|
'<ul><li>sample list</li></ul><ol><li>sample ordered item</li></ol>', |
|
86
|
|
|
'<ul id="a"><li id="c">sample list</li></ul><ol class="b"><li class="d">sample ordered item</li></ol>', |
|
87
|
|
|
'<div id="a"><div id="c">sample list</div></div><div class="b"><div class="d">sample ordered item</div></div>', |
|
88
|
|
|
]; |
|
89
|
|
|
/** |
|
90
|
|
|
* @var \DOMElement[] |
|
91
|
|
|
*/ |
|
92
|
|
|
private $samplesDocs = []; |
|
93
|
|
|
|
|
94
|
|
|
public function __construct(?string $name = null, array $data = [], string $dataName = '') |
|
95
|
|
|
{ |
|
96
|
|
|
parent::__construct($name, $data, $dataName); |
|
97
|
|
|
$this->rA = new Recursive(new Config($this->testA)); |
|
98
|
|
|
$this->rB = new Recursive(new Config($this->testB)); |
|
99
|
|
|
$this->rC = new Recursive(new Config($this->testC)); |
|
100
|
|
|
$pureConfig = \HTMLPurifier_Config::createDefault(); |
|
101
|
|
|
$pureConfig->set('Attr.EnableID', true); |
|
102
|
|
|
$this->docFactory = new DOMDocumentFactory( |
|
103
|
|
|
(new DOMDocumentFactoryConfig())->setInputPurifier(new \HTMLPurifier($pureConfig)) |
|
104
|
|
|
); |
|
105
|
|
|
$this->samplesDocs = \array_map(function ($sample) { |
|
106
|
|
|
return $this->docFactory->getNode($sample); |
|
107
|
|
|
}, $this->samples); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
public function testIsParentA() |
|
111
|
|
|
{ |
|
112
|
|
|
$this->assertTrue($this->rA->isParent($this->samplesDocs[0]->childNodes->item(0))); |
|
113
|
|
|
$this->assertTrue($this->rA->isParent($this->samplesDocs[0]->childNodes->item(1))); |
|
114
|
|
|
$this->assertTrue($this->rA->isParent($this->samplesDocs[1]->childNodes->item(0))); |
|
115
|
|
|
$this->assertTrue($this->rA->isParent($this->samplesDocs[1]->childNodes->item(1))); |
|
116
|
|
|
$this->assertFalse($this->rA->isParent($this->samplesDocs[2]->childNodes->item(0))); |
|
117
|
|
|
$this->assertFalse($this->rA->isParent($this->samplesDocs[2]->childNodes->item(1))); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
public function testIsParentB() |
|
121
|
|
|
{ |
|
122
|
|
|
$this->assertFalse($this->rB->isParent($this->samplesDocs[0]->childNodes->item(0))); |
|
123
|
|
|
$this->assertFalse($this->rB->isParent($this->samplesDocs[0]->childNodes->item(1))); |
|
124
|
|
|
$this->assertTrue($this->rB->isParent($this->samplesDocs[1]->childNodes->item(0))); |
|
125
|
|
|
$this->assertTrue($this->rB->isParent($this->samplesDocs[1]->childNodes->item(1))); |
|
126
|
|
|
$this->assertTrue($this->rB->isParent($this->samplesDocs[2]->childNodes->item(0))); |
|
127
|
|
|
$this->assertTrue($this->rB->isParent($this->samplesDocs[2]->childNodes->item(1))); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
public function testIsParentC() |
|
131
|
|
|
{ |
|
132
|
|
|
$this->assertTrue($this->rC->isParent($this->samplesDocs[0]->childNodes->item(0))); |
|
133
|
|
|
$this->assertTrue($this->rC->isParent($this->samplesDocs[0]->childNodes->item(1))); |
|
134
|
|
|
$this->assertTrue($this->rC->isParent($this->samplesDocs[1]->childNodes->item(0))); |
|
135
|
|
|
$this->assertTrue($this->rC->isParent($this->samplesDocs[1]->childNodes->item(1))); |
|
136
|
|
|
$this->assertTrue($this->rC->isParent($this->samplesDocs[2]->childNodes->item(0))); |
|
137
|
|
|
$this->assertTrue($this->rC->isParent($this->samplesDocs[2]->childNodes->item(1))); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
public function testIsChildA() |
|
141
|
|
|
{ |
|
142
|
|
|
$this->assertTrue($this->rA->isChild($this->samplesDocs[0]->childNodes->item(0)->firstChild)); |
|
143
|
|
|
$this->assertTrue($this->rA->isChild($this->samplesDocs[0]->childNodes->item(1)->firstChild)); |
|
144
|
|
|
$this->assertTrue($this->rA->isChild($this->samplesDocs[1]->childNodes->item(0)->firstChild)); |
|
145
|
|
|
$this->assertTrue($this->rA->isChild($this->samplesDocs[1]->childNodes->item(1)->firstChild)); |
|
146
|
|
|
$this->assertFalse($this->rA->isChild($this->samplesDocs[2]->childNodes->item(0)->firstChild)); |
|
147
|
|
|
$this->assertFalse($this->rA->isChild($this->samplesDocs[2]->childNodes->item(1)->firstChild)); |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
public function testIsChildB() |
|
151
|
|
|
{ |
|
152
|
|
|
$this->assertFalse($this->rB->isChild($this->samplesDocs[0]->childNodes->item(0)->firstChild)); |
|
153
|
|
|
$this->assertFalse($this->rB->isChild($this->samplesDocs[0]->childNodes->item(1)->firstChild)); |
|
154
|
|
|
$this->assertTrue($this->rB->isChild($this->samplesDocs[1]->childNodes->item(0)->firstChild)); |
|
155
|
|
|
$this->assertTrue($this->rB->isChild($this->samplesDocs[1]->childNodes->item(1)->firstChild)); |
|
156
|
|
|
$this->assertTrue($this->rB->isChild($this->samplesDocs[2]->childNodes->item(0)->firstChild)); |
|
157
|
|
|
$this->assertTrue($this->rB->isChild($this->samplesDocs[2]->childNodes->item(1)->firstChild)); |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
public function testIsChildC() |
|
161
|
|
|
{ |
|
162
|
|
|
$this->assertTrue($this->rC->isChild($this->samplesDocs[0]->childNodes->item(0)->firstChild)); |
|
163
|
|
|
$this->assertTrue($this->rC->isChild($this->samplesDocs[0]->childNodes->item(1)->firstChild)); |
|
164
|
|
|
$this->assertTrue($this->rC->isChild($this->samplesDocs[1]->childNodes->item(0)->firstChild)); |
|
165
|
|
|
$this->assertTrue($this->rC->isChild($this->samplesDocs[1]->childNodes->item(1)->firstChild)); |
|
166
|
|
|
$this->assertTrue($this->rC->isChild($this->samplesDocs[2]->childNodes->item(0)->firstChild)); |
|
167
|
|
|
$this->assertTrue($this->rC->isChild($this->samplesDocs[2]->childNodes->item(1)->firstChild)); |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
public function testGetParents() |
|
171
|
|
|
{ |
|
172
|
|
|
$this->assertEquals($this->testA[0]['parents']['tags'], $this->rA->getParents()['tags']); |
|
173
|
|
|
$this->assertEmpty($this->rA->getParents()['attributes']); |
|
174
|
|
|
$this->assertEmpty($this->rB->getParents()['tags']); |
|
175
|
|
|
$this->assertEquals($this->testB[0]['parents']['attributes'], $this->rB->getParents()['attributes']); |
|
176
|
|
|
$this->assertEquals($this->testC[0]['parents']['tags'], $this->rC->getParents()['tags']); |
|
177
|
|
|
$this->assertEquals($this->testC[0]['parents']['attributes'], $this->rC->getParents()['attributes']); |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
public function testGetChildren() |
|
181
|
|
|
{ |
|
182
|
|
|
$this->assertEquals($this->testA[0]['children']['tags'], $this->rA->getChildren()['tags']); |
|
183
|
|
|
$this->assertEmpty($this->rA->getChildren()['attributes']); |
|
184
|
|
|
$this->assertEmpty($this->rB->getChildren()['tags']); |
|
185
|
|
|
$this->assertEquals($this->testB[0]['children']['attributes'], $this->rB->getChildren()['attributes']); |
|
186
|
|
|
$this->assertEquals($this->testC[0]['children']['tags'], $this->rC->getChildren()['tags']); |
|
187
|
|
|
$this->assertEquals($this->testC[0]['children']['attributes'], $this->rC->getChildren()['attributes']); |
|
188
|
|
|
} |
|
189
|
|
|
} |
|
190
|
|
|
|