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
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.
13 * Copyright (C) 2004-2013 SugarCRM Inc. All rights reserved.
14 ********************************************************************************/
20 * Cron job hangs over minutes with CPU 100% during Email Import
24 class HTMLPurifier_ArrayTest extends Sugar_PHPUnit_Framework_TestCase
27 * Data provider for the rest of tests
30 public function getData()
34 array(array(1, 2, 3, 4))
39 * Testing of initialization of properties of HTMLPurifier_Array
41 * @dataProvider getData
43 public function testConstruct($array)
45 $object = new HTMLPurifier_ArrayMock($array);
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());
54 * Testing of offset & offsetItem properties while seeking/removing/inserting
56 public function testFindIndex()
58 $array = array(1, 2, 3, 4, 5);
59 $object = new HTMLPurifier_ArrayMock($array);
60 for ($i = 0; $i < $object->getCount(); $i ++) {
62 $this->assertEquals($i, $object->getOffset());
63 $this->assertEquals($array[$i], $object->getOffsetItem()->value);
67 $this->assertEquals(2, $object->getOffset());
68 $this->assertEquals(3, $object->getOffsetItem()->value);
70 $this->assertEquals(2, $object->getOffset());
71 $this->assertEquals(4, $object->getOffsetItem()->value);
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);
82 * Testing that behavior of insertBefore the same as array_splice
84 * @dataProvider getData
86 public function testInsertBefore($array)
88 $object = new HTMLPurifier_ArrayMock($array);
91 array_splice($array, $index, 0, array('a'));
92 $object->insertBefore($index, 'a');
93 $this->assertEquals($array, $object->getArray());
96 array_splice($array, $index, 0, array('a'));
97 $object->insertBefore($index, 'a');
98 $this->assertEquals($array, $object->getArray());
100 $index = count($array) * 2;
101 array_splice($array, $index, 0, array('a'));
102 $object->insertBefore($index, 'a');
103 $this->assertEquals($array, $object->getArray());
107 * Testing that behavior of remove the same as array_splice
109 * @dataProvider getData
111 public function testRemove($array)
113 $object = new HTMLPurifier_ArrayMock($array);
116 array_splice($array, $index, 1);
117 $object->remove($index);
118 $this->assertEquals($array, $object->getArray());
121 array_splice($array, $index, 1);
122 $object->remove($index);
123 $this->assertEquals($array, $object->getArray());
125 $index = count($array) * 2;
126 array_splice($array, $index, 1);
127 $object->remove($index);
128 $this->assertEquals($array, $object->getArray());
132 * Testing that behavior of arraySplice the same as array_splice
134 * @dataProvider getData
136 public function testArraySplice($array)
138 $object = new HTMLPurifier_ArrayMock($array);
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());
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());
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());
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());
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());
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());
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());
186 * Testing that object returns original array
188 * @dataProvider getData
190 public function testGetArray($array)
192 $object = new HTMLPurifier_ArrayMock($array);
193 $this->assertEquals($array, $object->getArray());
197 * Testing ArrayAccess interface
199 * @dataProvider getData
201 public function testOffsetExists($array)
203 $object = new HTMLPurifier_ArrayMock($array);
204 $this->assertEquals(isset($array[0]), isset($object[0]));
208 * Testing ArrayAccess interface
210 public function testOffsetGet()
212 $array = array(1, 2, 3);
213 $object = new HTMLPurifier_ArrayMock($array);
214 foreach ($array as $k => $v) {
215 $this->assertEquals($v, $object[$k]);
220 * Testing ArrayAccess interface
222 public function testOffsetSet()
224 $array = array(1, 2, 3);
225 $object = new HTMLPurifier_ArrayMock($array);
226 foreach ($array as $k => $v) {
229 $this->assertEquals($v, $object[$k]);
234 * Testing ArrayAccess interface
235 * There is one difference: keys are updated as well, they are started from 0
237 public function testOffsetUnset()
239 $object = new HTMLPurifier_ArrayMock(array(1, 2, 3, 4));
241 $this->assertEquals(array(1, 3, 4), $object->getArray());
243 $this->assertEquals(array(3, 4), $object->getArray());
245 $this->assertEquals(array(3), $object->getArray());
247 $this->assertEquals(array(), $object->getArray());
251 * Testing behavior when Array goes to zero size
253 public function testZeroSize()
255 $object = new HTMLPurifier_ArrayMock(array(1));
258 $this->assertNull($object->getHead());
259 $this->assertNull($object->getOffsetItem());
260 $this->assertEquals(0, $object->getCount());
261 $this->assertEquals(array(), $object->getArray());
263 $object->insertBefore(0, 1);
264 $this->assertEquals(array(1), $object->getArray());
269 * Mock for some protected properties of HTMLPurifier_Array
271 class HTMLPurifier_ArrayMock extends HTMLPurifier_Array
274 * @return HTMLPurifier_ArrayNode|null
276 public function getHead()
284 public function getOffset()
286 return $this->offset;
292 public function getCount()
298 * @return HTMLPurifier_ArrayNode|null
300 public function getOffsetItem()
302 return $this->offsetItem;