diff options
Diffstat (limited to 'engine/tests/api')
-rw-r--r-- | engine/tests/api/helpers.php | 323 |
1 files changed, 318 insertions, 5 deletions
diff --git a/engine/tests/api/helpers.php b/engine/tests/api/helpers.php index 461627547..cceb762be 100644 --- a/engine/tests/api/helpers.php +++ b/engine/tests/api/helpers.php @@ -31,6 +31,7 @@ class ElggCoreHelpersTest extends ElggCoreUnitTest { global $CONFIG; unset($CONFIG->externals); + unset($CONFIG->externals_priorities); } /** @@ -106,7 +107,9 @@ class ElggCoreHelpersTest extends ElggCoreUnitTest { // specify name $result = elgg_register_js('key', 'http://test1.com', 'footer'); $this->assertTrue($result); - $this->assertIdentical('http://test1.com', $CONFIG->externals['js']['key']->url); + $this->assertTrue(isset($CONFIG->externals_priorities['js']['key'])); + $index = $CONFIG->externals_priorities['js']['key']; + $this->assertIdentical('http://test1.com', $CONFIG->externals['js'][$index]->url); // send a bad url $result = @elgg_register_js('bad'); @@ -122,7 +125,9 @@ class ElggCoreHelpersTest extends ElggCoreUnitTest { // specify name $result = elgg_register_css('key', 'http://test1.com'); $this->assertTrue($result); - $this->assertIdentical('http://test1.com', $CONFIG->externals['css']['key']->url); + $this->assertTrue(isset($CONFIG->externals_priorities['css']['key'])); + $index = elgg_get_external_file_priority('css', 'key'); + $this->assertIdentical('http://test1.com', $CONFIG->externals['css'][$index]->url); } /** @@ -140,7 +145,13 @@ class ElggCoreHelpersTest extends ElggCoreUnitTest { $result = elgg_unregister_js('id1'); $this->assertTrue($result); - @$this->assertNULL($CONFIG->externals['js']['head']['id1']); + + $js = $CONFIG->externals['js']; + $elements = $js->getElements(); + $this->assertFalse(isset($CONFIG->externals_priorities['js']['id1'])); + foreach ($elements as $element) { + $this->assertFalse($element->name == 'id1'); + } $result = elgg_unregister_js('id1'); $this->assertFalse($result); @@ -148,7 +159,15 @@ class ElggCoreHelpersTest extends ElggCoreUnitTest { $this->assertFalse($result); $result = elgg_unregister_js('id2'); - $this->assertIdentical($urls['id3'], $CONFIG->externals['js']['id3']->url); + $elements = $js->getElements(); + $this->assertFalse(isset($CONFIG->externals_priorities['js']['id2'])); + foreach ($elements as $element) { + $this->assertFalse($element->name == 'id2'); + } + + $this->assertTrue(isset($CONFIG->externals_priorities['js']['id3'])); + $priority = $CONFIG->externals_priorities['js']['id3']; + $this->assertIdentical($urls['id3'], $CONFIG->externals['js'][$priority]->url); } /** @@ -187,4 +206,298 @@ class ElggCoreHelpersTest extends ElggCoreUnitTest { $js_urls = elgg_get_loaded_js('footer'); $this->assertIdentical(array(), $js_urls); } -} + + // test ElggPriorityList + public function testElggPriorityListAdd() { + $pl = new ElggPriorityList(); + $elements = array( + 'Test value', + 'Test value 2', + 'Test value 3' + ); + + shuffle($elements); + + foreach ($elements as $element) { + $this->assertTrue($pl->add($element) !== false); + } + + $test_elements = $pl->getElements(); + + $this->assertTrue(is_array($test_elements)); + + foreach ($test_elements as $i => $element) { + // should be in the array + $this->assertTrue(in_array($element, $elements)); + + // should be the only element, so priority 0 + $this->assertEqual($i, array_search($element, $elements)); + } + } + + public function testElggPriorityListAddWithPriority() { + $pl = new ElggPriorityList(); + + $elements = array( + 10 => 'Test Element 10', + 5 => 'Test Element 5', + 0 => 'Test Element 0', + 100 => 'Test Element 100', + -1 => 'Test Element -1', + -5 => 'Test Element -5' + ); + + foreach ($elements as $priority => $element) { + $pl->add($element, $priority); + } + + $test_elements = $pl->getElements(); + + // should be sorted by priority + $elements_sorted = array( + -5 => 'Test Element -5', + -1 => 'Test Element -1', + 0 => 'Test Element 0', + 5 => 'Test Element 5', + 10 => 'Test Element 10', + 100 => 'Test Element 100', + ); + + $this->assertIdentical($elements_sorted, $test_elements); + + foreach ($test_elements as $priority => $element) { + $this->assertIdentical($elements[$priority], $element); + } + } + + public function testElggPriorityListGetNextPriority() { + $pl = new ElggPriorityList(); + + $elements = array( + 2 => 'Test Element', + 0 => 'Test Element 2', + -2 => 'Test Element 3', + ); + + foreach ($elements as $priority => $element) { + $pl->add($element, $priority); + } + + // we're not specifying a priority so it should be the next consecutive to 0. + $this->assertEqual(1, $pl->getNextPriority()); + + // add another one at priority 1 + $pl->add('Test Element 1'); + + // next consecutive to 0 is now 3. + $this->assertEqual(3, $pl->getNextPriority()); + } + + public function testElggPriorityListRemove() { + $pl = new ElggPriorityList(); + + $elements = array(); + for ($i=0; $i<3; $i++) { + $element = new stdClass(); + $element->name = "Test Element $i"; + $element->someAttribute = rand(0, 9999); + $elements[] = $element; + $pl->add($element); + } + + $pl->remove($elements[1]); + + $test_elements = $pl->getElements(); + + // make sure it's gone. + $this->assertTrue(2, count($test_elements)); + $this->assertIdentical($elements[0], $test_elements[0]); + $this->assertIdentical($elements[2], $test_elements[2]); + } + + public function testElggPriorityListConstructor() { + $elements = array( + 10 => 'Test Element 10', + 5 => 'Test Element 5', + 0 => 'Test Element 0', + 100 => 'Test Element 100', + -1 => 'Test Element -1', + -5 => 'Test Element -5' + ); + + $pl = new ElggPriorityList($elements); + $test_elements = $pl->getElements(); + + $elements_sorted = array( + -5 => 'Test Element -5', + -1 => 'Test Element -1', + 0 => 'Test Element 0', + 5 => 'Test Element 5', + 10 => 'Test Element 10', + 100 => 'Test Element 100', + ); + + $this->assertIdentical($elements_sorted, $test_elements); + } + + public function testElggPriorityListGetPriority() { + $pl = new ElggPriorityList(); + + $elements = array( + 'Test element 0', + 'Test element 1', + 'Test element 2', + ); + + foreach ($elements as $element) { + $pl->add($element); + } + + $this->assertIdentical(0, $pl->getPriority($elements[0])); + $this->assertIdentical(1, $pl->getPriority($elements[1])); + $this->assertIdentical(2, $pl->getPriority($elements[2])); + } + + public function testElggPriorityListPriorityCollision() { + $pl = new ElggPriorityList(); + + $elements = array( + 5 => 'Test element 5', + 6 => 'Test element 6', + 0 => 'Test element 0', + ); + + foreach ($elements as $priority => $element) { + $pl->add($element, $priority); + } + + // add at a colliding priority + $pl->add('Colliding element', 5); + + // should float to the top closest to 5, so 7 + $this->assertEqual(7, $pl->getPriority('Colliding element')); + } + + public function testElggPriorityListArrayAccess() { + $pl = new ElggPriorityList(); + + $pl[] = 'Test element 0'; + $pl[-10] = 'Test element -10'; + $pl[-1] = 'Test element -1'; + $pl[] = 'Test element 1'; + $pl[5] = 'Test element 5'; + $pl[0] = 'Test element collision with 0'; + + $elements = array( + -1 => 'Test element -1', + 0 => 'Test element collision with 0', + 1 => 'Test element 0', + 2 => 'Test element 1', + 5 => 'Test element 5', + ); + + $priority = $pl->getPriority('Test element -10'); + unset($pl[$priority]); + + $test_elements = $pl->getElements(); + $this->assertIdentical($elements, $test_elements); + } + + public function testElggPriorityListIterator() { + $elements = array( + -5 => 'Test element -5', + 0 => 'Test element 0', + 5 => 'Test element 5' + ); + + $pl = new ElggPriorityList($elements); + + foreach ($pl as $priority => $element) { + $this->assertIdentical($elements[$priority], $element); + } + } + + public function testElggPriorityListCountable() { + $pl = new ElggPriorityList(); + + $this->assertEqual(0, count($pl)); + + $pl[] = 'Test element 0'; + $this->assertEqual(1, count($pl)); + + $pl[] = 'Test element 1'; + $this->assertEqual(2, count($pl)); + + $pl[] = 'Test element 2'; + $this->assertEqual(3, count($pl)); + } + + public function testElggPriorityListUserSort() { + $elements = array( + 'A', + 'B', + 'C', + 'D', + 'E', + ); + + $elements_sorted_string = $elements; + + shuffle($elements); + $pl = new ElggPriorityList($elements); + + // will sort by priority + $test_elements = $pl->getElements(); + $this->assertIdentical($elements, $test_elements); + + function test_sort($elements) { + sort($elements, SORT_LOCALE_STRING); + return $elements; + } + + // force a new sort using our function + $pl->sort('test_sort'); + $test_elements = $pl->getElements(); + + $this->assertIdentical($elements_sorted_string, $test_elements); + } + + function testElggPriorityListShiftElementsSegment() { + $elements = array( + 0 => 'Element 0', + 1 => 'Element 1', + 2 => 'Element 2', + 4 => 'Element 4', + ); + + $pl = new ElggPriorityList($elements); + + // add a new element directly at 1. + $pl->add('New Element', 1, true); + + $elements_sorted = array( + 0 => 'Element 0', + 1 => 'New Element', + 2 => 'Element 1', + 3 => 'Element 2', + 4 => 'Element 4', + ); + + $test_elements = $pl->getElements(); + $this->assertIdentical($elements_sorted, $test_elements); + + $pl->add('New Element 10', 10, true); + + $elements_sorted = array( + 0 => 'Element 0', + 1 => 'New Element', + 2 => 'Element 1', + 3 => 'Element 2', + 4 => 'Element 4', + 10 => 'New Element 10' + ); + + $test_elements = $pl->getElements(); + $this->assertIdentical($elements_sorted, $test_elements); + } +}
\ No newline at end of file |