]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - tests/include/HTMLPurifier/HTMLPurifier_ArrayTest.php
Release 6.5.16
[Github/sugarcrm.git] / tests / include / HTMLPurifier / HTMLPurifier_ArrayTest.php
1 <?php
2
3 /*********************************************************************************
4  * By installing or using this file, you are confirming on behalf of the entity
5  * subscribed to the SugarCRM Inc. product ("Company") that Company is bound by
6  * the SugarCRM Inc. Master Subscription Agreement (“MSA”), which is viewable at:
7  * http://www.sugarcrm.com/master-subscription-agreement
8  *
9  * If Company is not bound by the MSA, then by installing or using this file
10  * you are agreeing unconditionally that Company will be bound by the MSA and
11  * certifying that you have authority to bind Company accordingly.
12  *
13  * Copyright (C) 2004-2013 SugarCRM Inc.  All rights reserved.
14  ********************************************************************************/
15
16
17
18 /**
19  * Bug #61818
20  * Cron job hangs over minutes with CPU 100% during Email Import
21  *
22  * @ticket 61818
23  */
24 class HTMLPurifier_ArrayTest extends Sugar_PHPUnit_Framework_TestCase
25 {
26     /**
27      * Data provider for the rest of tests
28      * @return array
29      */
30     public function getData()
31     {
32         return array(
33             array(array()),
34             array(array(1, 2, 3, 4))
35         );
36     }
37
38     /**
39      * Testing of initialization of properties of HTMLPurifier_Array
40      *
41      * @dataProvider getData
42      */
43     public function testConstruct($array)
44     {
45         $object = new HTMLPurifier_ArrayMock($array);
46
47         $this->assertEquals(0, $object->getOffset());
48         $this->assertEquals($object->getHead(), $object->getOffsetItem());
49         $this->assertEquals(count($array), $object->getCount());
50         $this->assertEquals($array, $object->getArray());
51     }
52
53     /**
54      * Testing of offset & offsetItem properties while seeking/removing/inserting
55      */
56     public function testFindIndex()
57     {
58         $array = array(1, 2, 3, 4, 5);
59         $object = new HTMLPurifier_ArrayMock($array);
60         for ($i = 0; $i < $object->getCount(); $i ++) {
61             $object[$i];
62             $this->assertEquals($i, $object->getOffset());
63             $this->assertEquals($array[$i], $object->getOffsetItem()->value);
64         }
65
66         $object[2];
67         $this->assertEquals(2, $object->getOffset());
68         $this->assertEquals(3, $object->getOffsetItem()->value);
69         $object->remove(2);
70         $this->assertEquals(2, $object->getOffset());
71         $this->assertEquals(4, $object->getOffsetItem()->value);
72
73         $object[1];
74         $this->assertEquals(1, $object->getOffset());
75         $this->assertEquals(2, $object->getOffsetItem()->value);
76         $object->insertBefore(1, 'a');
77         $this->assertEquals(1, $object->getOffset());
78         $this->assertEquals('a', $object->getOffsetItem()->value);
79     }
80
81     /**
82      * Testing that behavior of insertBefore the same as array_splice
83      *
84      * @dataProvider getData
85      */
86     public function testInsertBefore($array)
87     {
88         $object = new HTMLPurifier_ArrayMock($array);
89
90         $index = 0;
91         array_splice($array, $index, 0, array('a'));
92         $object->insertBefore($index, 'a');
93         $this->assertEquals($array, $object->getArray());
94
95         $index = 2;
96         array_splice($array, $index, 0, array('a'));
97         $object->insertBefore($index, 'a');
98         $this->assertEquals($array, $object->getArray());
99
100         $index = count($array) * 2;
101         array_splice($array, $index, 0, array('a'));
102         $object->insertBefore($index, 'a');
103         $this->assertEquals($array, $object->getArray());
104     }
105
106     /**
107      * Testing that behavior of remove the same as array_splice
108      *
109      * @dataProvider getData
110      */
111     public function testRemove($array)
112     {
113         $object = new HTMLPurifier_ArrayMock($array);
114
115         $index = 0;
116         array_splice($array, $index, 1);
117         $object->remove($index);
118         $this->assertEquals($array, $object->getArray());
119
120         $index = 2;
121         array_splice($array, $index, 1);
122         $object->remove($index);
123         $this->assertEquals($array, $object->getArray());
124
125         $index = count($array) * 2;
126         array_splice($array, $index, 1);
127         $object->remove($index);
128         $this->assertEquals($array, $object->getArray());
129     }
130
131     /**
132      * Testing that behavior of arraySplice the same as array_splice
133      *
134      * @dataProvider getData
135      */
136     public function testArraySplice($array)
137     {
138         $object = new HTMLPurifier_ArrayMock($array);
139
140         $replacement = array(rand(10, 100), rand(10, 100), rand(10, 100));
141         $returnArray = array_splice($array, 0, 0, $replacement);
142         $returnObject = $object->splice(0, 0, $replacement);
143         $this->assertEquals($returnArray, $returnObject);
144         $this->assertEquals($array, $object->getArray());
145
146         $replacement = array(rand(10, 100), rand(10, 100), rand(10, 100));
147         $returnArray = array_splice($array, 0, 1, $replacement);
148         $returnObject = $object->splice(0, 1, $replacement);
149         $this->assertEquals($returnArray, $returnObject);
150         $this->assertEquals($array, $object->getArray());
151
152         $replacement = array(rand(10, 100), rand(10, 100), rand(10, 100));
153         $returnArray = array_splice($array, 1, 0, $replacement);
154         $returnObject = $object->splice(1, 0, $replacement);
155         $this->assertEquals($returnArray, $returnObject);
156         $this->assertEquals($array, $object->getArray());
157
158         $replacement = array(rand(10, 100), rand(10, 100), rand(10, 100));
159         $returnArray = array_splice($array, 1, 1, $replacement);
160         $returnObject = $object->splice(1, 1, $replacement);
161         $this->assertEquals($returnArray, $returnObject);
162         $this->assertEquals($array, $object->getArray());
163
164         $replacement = array(rand(10, 100), rand(10, 100), rand(10, 100));
165         $returnArray = array_splice($array, 1, 2, $replacement);
166         $returnObject = $object->splice(1, 2, $replacement);
167         $this->assertEquals($returnArray, $returnObject);
168         $this->assertEquals($array, $object->getArray());
169
170         $length = count($array) + 1;
171         $replacement = array(rand(10, 100), rand(10, 100), rand(10, 100));
172         $returnArray = array_splice($array, $length, 0, $replacement);
173         $returnObject = $object->splice($length, 0, $replacement);
174         $this->assertEquals($returnArray, $returnObject);
175         $this->assertEquals($array, $object->getArray());
176
177         $length = count($array) + 1;
178         $replacement = array(rand(10, 100), rand(10, 100), rand(10, 100));
179         $returnArray = array_splice($array, $length, 2, $replacement);
180         $returnObject = $object->splice($length, 2, $replacement);
181         $this->assertEquals($returnArray, $returnObject);
182         $this->assertEquals($array, $object->getArray());
183     }
184
185     /**
186      * Testing that object returns original array
187      *
188      * @dataProvider getData
189      */
190     public function testGetArray($array)
191     {
192         $object = new HTMLPurifier_ArrayMock($array);
193         $this->assertEquals($array, $object->getArray());
194     }
195
196     /**
197      * Testing ArrayAccess interface
198      *
199      * @dataProvider getData
200      */
201     public function testOffsetExists($array)
202     {
203         $object = new HTMLPurifier_ArrayMock($array);
204         $this->assertEquals(isset($array[0]), isset($object[0]));
205     }
206
207     /**
208      * Testing ArrayAccess interface
209      */
210     public function testOffsetGet()
211     {
212         $array = array(1, 2, 3);
213         $object = new HTMLPurifier_ArrayMock($array);
214         foreach ($array as $k => $v) {
215             $this->assertEquals($v, $object[$k]);
216         }
217     }
218
219     /**
220      * Testing ArrayAccess interface
221      */
222     public function testOffsetSet()
223     {
224         $array = array(1, 2, 3);
225         $object = new HTMLPurifier_ArrayMock($array);
226         foreach ($array as $k => $v) {
227             $v = $v * 2;
228             $object[$k] = $v;
229             $this->assertEquals($v, $object[$k]);
230         }
231     }
232
233     /**
234      * Testing ArrayAccess interface
235      * There is one difference: keys are updated as well, they are started from 0
236      */
237     public function testOffsetUnset()
238     {
239         $object = new HTMLPurifier_ArrayMock(array(1, 2, 3, 4));
240         unset($object[1]);
241         $this->assertEquals(array(1, 3, 4), $object->getArray());
242         unset($object[0]);
243         $this->assertEquals(array(3, 4), $object->getArray());
244         unset($object[1]);
245         $this->assertEquals(array(3), $object->getArray());
246         unset($object[0]);
247         $this->assertEquals(array(), $object->getArray());
248     }
249
250     /**
251      * Testing behavior when Array goes to zero size
252      */
253     public function testZeroSize()
254     {
255         $object = new HTMLPurifier_ArrayMock(array(1));
256
257         $object->remove(0);
258         $this->assertNull($object->getHead());
259         $this->assertNull($object->getOffsetItem());
260         $this->assertEquals(0, $object->getCount());
261         $this->assertEquals(array(), $object->getArray());
262
263         $object->insertBefore(0, 1);
264         $this->assertEquals(array(1), $object->getArray());
265     }
266 }
267
268 /**
269  * Mock for some protected properties of HTMLPurifier_Array
270  */
271 class HTMLPurifier_ArrayMock extends HTMLPurifier_Array
272 {
273     /**
274      * @return HTMLPurifier_ArrayNode|null
275      */
276     public function getHead()
277     {
278         return $this->head;
279     }
280
281     /**
282      * @return int
283      */
284     public function getOffset()
285     {
286         return $this->offset;
287     }
288
289     /**
290      * @return int
291      */
292     public function getCount()
293     {
294         return $this->count;
295     }
296
297     /**
298      * @return HTMLPurifier_ArrayNode|null
299      */
300     public function getOffsetItem()
301     {
302         return $this->offsetItem;
303     }
304 }