aboutsummaryrefslogtreecommitdiff
path: root/engine
diff options
context:
space:
mode:
authorBrett Profitt <brett.profitt@gmail.com>2011-08-23 15:46:22 -0700
committerBrett Profitt <brett.profitt@gmail.com>2011-08-23 15:46:22 -0700
commit5285471b7c6f2b4ef7dc02fb2a9c231e2216eef7 (patch)
tree207fb81c04dbb8d83cfed464f2cc0f417770c5fc /engine
parent3e1a96cabb228b59021676772daca23b22a026c7 (diff)
downloadelgg-5285471b7c6f2b4ef7dc02fb2a9c231e2216eef7.tar.gz
elgg-5285471b7c6f2b4ef7dc02fb2a9c231e2216eef7.tar.bz2
Fixes #3355. Added ElggPriorityList. Adapted the externals system to use it.
Diffstat (limited to 'engine')
-rw-r--r--engine/classes/ElggPriorityList.php218
-rw-r--r--engine/lib/elgglib.php139
-rw-r--r--engine/lib/views.php29
-rw-r--r--engine/tests/api/helpers.php167
4 files changed, 241 insertions, 312 deletions
diff --git a/engine/classes/ElggPriorityList.php b/engine/classes/ElggPriorityList.php
index 931138106..17ce228bc 100644
--- a/engine/classes/ElggPriorityList.php
+++ b/engine/classes/ElggPriorityList.php
@@ -2,40 +2,11 @@
/**
* Iterate over elements in a specific priority.
*
- * You can add, remove, and access elements using OOP or array interfaces:
- *
- * // OOP
* $pl = new ElggPriorityList();
* $pl->add('Element 0');
- * $pl->add('Element -5', -5);
* $pl->add('Element 10', 10);
* $pl->add('Element -10', -10);
*
- * $pl->remove('Element -5');
- *
- * $elements = $pl->getElements();
- * var_dump($elements);
- *
- * Yields:
- *
- * array(
- * -10 => 'Element -10',
- * 0 => 'Element 0',
- * 10 => 'Element 10',
- * )
- *
- *
- * // Array
- *
- * $pl = new ElggPriorityList();
- * $pl[] = 'Element 0';
- * $pl[-5] = 'Element -5';
- * $pl[10] = 'Element 10';
- * $pl[-10] = 'Element -10';
- *
- * $priority = $pl->getPriority('Element -5');
- * unset($pl[$priority]);
- *
* foreach ($pl as $priority => $element) {
* var_dump("$priority => $element");
* }
@@ -45,52 +16,84 @@
* 0 => Element 0
* 10 => Element 10
*
- *
- * Collisions with priority are handled by default differently in the OOP and the array interfaces.
- *
- * If using the OOP interface, the default is to insert the element as close to the requested
- * priority as possible.
+ * Collisions on priority are handled by inserting the element at or as close to the
+ * requested priority as possible:
*
* $pl = new ElggPriorityList();
* $pl->add('Element 5', 5);
* $pl->add('Colliding element 5', 5);
* $pl->add('Another colliding element 5', 5);
*
- * var_dump($pl->getElements());
+ * foreach ($pl as $priority => $element) {
+ * var_dump("$priority => $element");
+ * }
*
* Yields:
- * array(
* 5 => 'Element 5',
* 6 => 'Colliding element 5',
* 7 => 'Another colliding element 5'
- * )
*
- * If using the array interface, elements are added at exactly the priority, displacing other
- * elements if necessary. This behavior is also available by passing true as the 3rd argument to
- * ->add():
+ * You can do priority lookups by element:
*
* $pl = new ElggPriorityList();
- * $pl[5] = 'Element 5';
- * $pl[6] = 'Element 6';
- * $pl[5] = 'Colliding element 5'; // shifts the previous two up by one
- * $pl->add('Another colliding element 5', 5, true); // shifts the previous three up by one
+ * $pl->add('Element 0');
+ * $pl->add('Element -5', -5);
+ * $pl->add('Element 10', 10);
+ * $pl->add('Element -10', -10);
*
- * var_dump($pl->getElements());
+ * $priority = $pl->getPriority('Element -5');
+ *
+ * Or element lookups by priority.
+ * $element = $pl->getElement(-5);
*
- * Yields:
- * array(
- * 5 => 'Another colliding element 5',
- * 6 => 'Colliding element 5',
- * 7 => 'Element 5',
- * 8 => 'Element 6'
- * )
+ * To remove elements, pass the element.
+ * $pl->remove('Element -10');
+ *
+ * To check if an element exists:
+ * $pl->contains('Element -5');
+ *
+ * To move an element:
+ * $pl->move('Element -5', -3);
+ *
+ * ElggPriorityList only tracks priority. No checking is done in ElggPriorityList for duplicates or
+ * updating. If you need to track this use objects and an external map:
+ *
+ * function elgg_register_something($id, $display_name, $location, $priority = 500) {
+ * // $id => $element.
+ * static $map = array();
+ * static $list;
+ *
+ * if (!$list) {
+ * $list = new ElggPriorityList();
+ * }
+ *
+ * // update if already registered.
+ * if (isset($map[$id])) {
+ * $element = $map[$id];
+ * // move it first because we have to pass the original element.
+ * if (!$list->move($element, $priority)) {
+ * return false;
+ * }
+ * $element->display_name = $display_name;
+ * $element->location = $location;
+ * } else {
+ * $element = new stdClass();
+ * $element->display_name = $display_name;
+ * $element->location = $location;
+ * if (!$list->add($element, $priority)) {
+ * return false;
+ * }
+ * $map[$id] = $element;
+ * }
+ *
+ * return true;
+ * }
*
* @package Elgg.Core
* @subpackage Helpers
*/
-
class ElggPriorityList
- implements Iterator, ArrayAccess, Countable {
+ implements Iterator, Countable {
/**
* The list of elements
@@ -123,15 +126,11 @@ class ElggPriorityList
* maintains its priority and the new element is to the next available
* slot, taking into consideration all previously registered elements.
* Negative elements are accepted.
- * @param bool $exact If true, will put the element at exactly the priority specified, displacing
- * other elements.
* @return int The priority of the added element.
*/
public function add($element, $priority = null, $exact = false) {
if ($priority !== null && !is_numeric($priority)) {
return false;
- } elseif ($exact) {
- $this->shiftElementsSegment($priority);
} else {
$priority = $this->getNextPriority($priority);
}
@@ -163,16 +162,16 @@ class ElggPriorityList
/**
* Move an existing element to a new priority.
*
- * @param int $current_priority
- * @param int $new_priority
- * @param bool $exact
- * @return bool
+ * @param mixed $current_priority
+ * @param int $new_priority
+ *
+ * @return int The new priority.
*/
- public function move($current_priority, $new_priority, $exact = false) {
- $current_priority = (int) $current_priority;
+ public function move($element, $new_priority, $strict = false) {
$new_priority = (int) $new_priority;
-
- if (!isset($this->elements[$current_priority])) {
+
+ $current_priority = $this->getPriority($element, $strict);
+ if (!$current_priority) {
return false;
}
@@ -180,17 +179,16 @@ class ElggPriorityList
return true;
}
- $element = $this->elements[$current_priority];
+ // move the actual element so strict operations still work
+ $element = $this->getElement($current_priority);
unset($this->elements[$current_priority]);
-
- return $this->add($element, $new_priority, $exact);
+ return $this->add($element, $new_priority);
}
/**
* Returns the elements
*
- * @param type $elements
- * @param type $sort
+ * @return array
*/
public function getElements() {
$this->sortIfUnsorted();
@@ -239,29 +237,6 @@ class ElggPriorityList
}
/**
- * Shift a segment of elements starting at $index up by one until the end of the array or
- * there's a gap in the indexes. This produces a space at $index to insert a new element.
- *
- * @param type $index The index to start
- * @return array
- */
- private function shiftElementsSegment($index) {
- $index = (int) $index;
- // @todo probably a better way.
- $replace_elements = array();
- while (isset($this->elements[$index])) {
- $replace_elements[$index + 1] = $this->elements[$index];
- unset($this->elements[$index]);
- $index++;
- }
-
- // insert old ones
- foreach ($replace_elements as $index => $element) {
- $this->elements[$index] = $element;
- }
- }
-
- /**
* Returns the next priority available.
*
* @param int $near Make the priority as close to $near as possible.
@@ -277,25 +252,45 @@ class ElggPriorityList
return $near;
}
-
/**
* Returns the priority of an element if it exists in the list.
*
- * @warning This can return 0 if the element's priority is 0. Use identical operator (===) to
- * check for false if you want to know if an element exists.
+ * @warning This can return 0 if the element's priority is 0.
*
- * @param mixed $element
+ * @param mixed $element The element to check for.
+ * @param bool $strict Use strict checking?
* @return mixed False if the element doesn't exists, the priority if it does.
*/
public function getPriority($element, $strict = false) {
return array_search($element, $this->elements, $strict);
}
+ /**
+ * Returns the element at $priority.
+ *
+ * @param int $priority
+ * @return mixed The element or false on fail.
+ */
+ public function getElement($priority) {
+ return (isset($this->elements[$priority])) ? $this->elements[$priority] : false;
+ }
+
+ /**
+ * Returns if the list contains $element.
+ *
+ * @param mixed $element The element to check.
+ * @param bool $strict Use strict checking?
+ * @return bool
+ */
+ public function contains($element, $strict = false) {
+ return $this->getPriority($element, $strict) !== false;
+ }
+
+
/**********************
- * Interfaces methods *
+ * Interface methods *
**********************/
-
/**
* Iterator
*/
@@ -356,29 +351,8 @@ class ElggPriorityList
return ($key !== NULL && $key !== FALSE);
}
- // Coutable
+ // Countable
public function count() {
return count($this->elements);
}
-
- // ArrayAccess
- public function offsetExists($offset) {
- return isset($this->elements[$offset]);
- }
-
- public function offsetGet($offset) {
- return isset($this->elements[$offset]) ? $this->elements[$offset] : null;
- }
-
- public function offsetSet($offset, $value) {
- // for $pl[] = 'New element'
- $exact = ($offset !== null);
- return $this->add($value, $offset, $exact);
- }
-
- public function offsetUnset($offset) {
- if (isset($this->elements[$offset])) {
- unset($this->elements[$offset]);
- }
- }
} \ No newline at end of file
diff --git a/engine/lib/elgglib.php b/engine/lib/elgglib.php
index b6b603e79..198ffe60c 100644
--- a/engine/lib/elgglib.php
+++ b/engine/lib/elgglib.php
@@ -167,7 +167,7 @@ function forward($location = "", $reason = 'system') {
* @param string $name An identifier for the JavaScript library
* @param string $url URL of the JavaScript file
* @param string $location Page location: head or footer. (default: head)
- * @param int $priority Priority of the CSS file (lower numbers load earlier)
+ * @param int $priority Priority of the JS file (lower numbers load earlier)
*
* @return bool
* @since 1.8.0
@@ -288,28 +288,24 @@ function elgg_register_external_file($type, $name, $url, $location, $priority =
$url = elgg_format_url($url);
$url = elgg_normalize_url($url);
- if (!isset($CONFIG->externals)) {
- $CONFIG->externals = array();
- }
-
- if (!$CONFIG->externals[$type] instanceof ElggPriorityList) {
- $CONFIG->externals[$type] = new ElggPriorityList();
- }
+ elgg_bootstrap_externals_data_structure($type);
$name = trim(strtolower($name));
$priority = max((int)$priority, 0);
+ $item = elgg_extract($name, $CONFIG->externals_map[$type]);
- $index = elgg_get_external_file_priority($name, $type);
-
- if ($index !== false) {
+ if ($item) {
// updating a registered item
- $item = $CONFIG->externals[$type][$index];
+ // don't update loaded because it could already be set
$item->url = $url;
$item->location = $location;
- // remove old saved priority
- elgg_remove_external_file_priority($name, $type);
- $priority = $CONFIG->externals[$type]->move($index, $priority);
+ // if loaded before registered, that means it hasn't been added to the list yet
+ if ($CONFIG->externals[$type]->contains($item)) {
+ $priority = $CONFIG->externals[$type]->move($item, $priority);
+ } else {
+ $priority = $CONFIG->externals[$type]->add($item, $priority);
+ }
} else {
$item = new stdClass();
$item->loaded = false;
@@ -319,10 +315,9 @@ function elgg_register_external_file($type, $name, $url, $location, $priority =
$priority = $CONFIG->externals[$type]->add($item, $priority);
}
- // save priority map so we can update if added again
- elgg_save_external_file_priority($priority, $name, $type);
+ $CONFIG->externals_map[$type][$name] = $item;
- return true;
+ return $priority !== false;
}
/**
@@ -337,22 +332,14 @@ function elgg_register_external_file($type, $name, $url, $location, $priority =
function elgg_unregister_external_file($type, $name) {
global $CONFIG;
- if (!isset($CONFIG->externals)) {
- return false;
- }
-
- if (!$CONFIG->externals[$type] instanceof ElggPriorityList) {
- return false;
- }
+ elgg_bootstrap_externals_data_structure($type);
$name = trim(strtolower($name));
-
- $priority = elgg_get_external_file_priority($name, $type);
+ $item = elgg_extract($name, $CONFIG->externals_map[$type]);
- if ($priority !== false) {
- elgg_remove_external_file_priority($name, $type);
- unset($CONFIG->externals[$type][$priority]);
- return true;
+ if ($item) {
+ unset($CONFIG->externals_map[$type][$name]);
+ return $CONFIG->externals[$type]->remove($item);
}
return false;
@@ -370,21 +357,15 @@ function elgg_unregister_external_file($type, $name) {
function elgg_load_external_file($type, $name) {
global $CONFIG;
- if (!isset($CONFIG->externals)) {
- $CONFIG->externals = array();
- }
-
- if (!$CONFIG->externals[$type] instanceof ElggPriorityList) {
- $CONFIG->externals[$type] = new ElggPriorityList();
- }
+ elgg_bootstrap_externals_data_structure($type);
$name = trim(strtolower($name));
- $priority = elgg_get_external_file_priority($name, $type);
+ $item = elgg_extract($name, $CONFIG->externals_map[$type]);
- if ($priority !== false) {
+ if ($item) {
// update a registered item
- $CONFIG->externals[$type][$priority]->loaded = true;
+ $item->loaded = true;
} else {
$item = new stdClass();
$item->loaded = true;
@@ -392,60 +373,11 @@ function elgg_load_external_file($type, $name) {
$item->location = '';
$priority = $CONFIG->externals[$type]->add($item);
- elgg_save_external_file_priority($priority, $name, $type);
+ $CONFIG->externals_map[$type][$name] = $item;
}
}
/**
- * Gets the priority of an external by name and type.
- *
- * @param type $name
- * @param type $type
- * @return type
- */
-function elgg_get_external_file_priority($name, $type) {
- global $CONFIG;
-
- if (!isset($CONFIG->externals_priorities[$type][$name])) {
- return false;
- }
-
- return $CONFIG->externals_priorities[$type][$name];
-}
-
-function elgg_save_external_file_priority($priority, $name, $type) {
- global $CONFIG;
-
- if (!isset($CONFIG->externals_priorities)) {
- $CONFIG->externals_priorities = array();
- }
-
- if (!isset($CONFIG->externals_priorities[$type])) {
- $CONFIG->externals_priorities[$type] = array();
- }
-
- $CONFIG->externals_priorities[$type][$name] = $priority;
-
- return true;
-}
-
-function elgg_remove_external_file_priority($name, $type) {
- global $CONFIG;
-
- if (!isset($CONFIG->externals_priorities)) {
- $CONFIG->externals_priorities = array();
- }
-
- if (!isset($CONFIG->externals_priorities[$type])) {
- $CONFIG->externals_priorities[$type] = array();
- }
-
- unset($CONFIG->externals_priorities[$type][$name]);
-
- return true;
-}
-
-/**
* Get external resource descriptors
*
* @param string $type Type of file: js or css
@@ -471,6 +403,31 @@ function elgg_get_loaded_external_files($type, $location) {
}
/**
+ * Bootstraps the externals data structure in $CONFIG.
+ *
+ * @param string $type The type of external, js or css.
+ */
+function elgg_bootstrap_externals_data_structure($type) {
+ global $CONFIG;
+
+ if (!isset($CONFIG->externals)) {
+ $CONFIG->externals = array();
+ }
+
+ if (!$CONFIG->externals[$type] instanceof ElggPriorityList) {
+ $CONFIG->externals[$type] = new ElggPriorityList();
+ }
+
+ if (!isset($CONFIG->externals_map)) {
+ $CONFIG->externals_map = array();
+ }
+
+ if (!isset($CONFIG->externals_map[$type])) {
+ $CONFIG->externals_map[$type] = array();
+ }
+}
+
+/**
* Returns a list of files in $directory.
*
* Only returns files. Does not recurse into subdirs.
diff --git a/engine/lib/views.php b/engine/lib/views.php
index 7686a8bef..fe3265347 100644
--- a/engine/lib/views.php
+++ b/engine/lib/views.php
@@ -1480,21 +1480,6 @@ function autoregister_views($view_base, $folder, $base_location_path, $viewtype)
}
/**
- * Add the core Elgg head elements that could be cached
- *
- * @return void
- */
-function elgg_views_register_core_head_elements() {
- $url = elgg_get_simplecache_url('js', 'elgg');
- elgg_register_js('elgg', $url, 'head', 10);
- elgg_load_js('elgg');
-
- $url = elgg_get_simplecache_url('css', 'elgg');
- elgg_register_css('elgg', $url, 10);
- elgg_load_css('elgg');
-}
-
-/**
* Add the rss link to the extras when if needed
*
* @return void
@@ -1548,12 +1533,17 @@ function elgg_views_boot() {
elgg_register_simplecache_view('css/ie6');
elgg_register_simplecache_view('js/elgg');
- elgg_register_js('jquery', '/vendors/jquery/jquery-1.6.2.min.js', 'head', 1);
- elgg_register_js('jquery-ui', '/vendors/jquery/jquery-ui-1.8.16.min.js', 'head', 2);
+ elgg_register_js('jquery', '/vendors/jquery/jquery-1.6.2.min.js', 'head');
+ elgg_register_js('jquery-ui', '/vendors/jquery/jquery-ui-1.8.16.min.js', 'head');
elgg_register_js('jquery.form', '/vendors/jquery/jquery.form.js');
+
+ $elgg_js_url = elgg_get_simplecache_url('js', 'elgg');
+ elgg_register_js('elgg', $elgg_js_url, 'head');
+
elgg_load_js('jquery');
elgg_load_js('jquery-ui');
elgg_load_js('jquery.form');
+ elgg_load_js('elgg');
elgg_register_simplecache_view('js/lightbox');
$lightbox_js_url = elgg_get_simplecache_url('js', 'lightbox');
@@ -1561,7 +1551,10 @@ function elgg_views_boot() {
$lightbox_css_url = 'vendors/jquery/fancybox/jquery.fancybox-1.3.4.css';
elgg_register_css('lightbox', $lightbox_css_url);
- elgg_register_event_handler('ready', 'system', 'elgg_views_register_core_head_elements');
+ $elgg_css_url = elgg_get_simplecache_url('css', 'elgg');
+ elgg_register_css('elgg', $elgg_css_url, 1);
+ elgg_load_css('elgg');
+
elgg_register_event_handler('pagesetup', 'system', 'elgg_views_add_rss_link');
// discover the built-in view types
diff --git a/engine/tests/api/helpers.php b/engine/tests/api/helpers.php
index cceb762be..ee2e64cfe 100644
--- a/engine/tests/api/helpers.php
+++ b/engine/tests/api/helpers.php
@@ -31,7 +31,7 @@ class ElggCoreHelpersTest extends ElggCoreUnitTest {
global $CONFIG;
unset($CONFIG->externals);
- unset($CONFIG->externals_priorities);
+ unset($CONFIG->externals_map);
}
/**
@@ -107,9 +107,16 @@ class ElggCoreHelpersTest extends ElggCoreUnitTest {
// specify name
$result = elgg_register_js('key', 'http://test1.com', 'footer');
$this->assertTrue($result);
- $this->assertTrue(isset($CONFIG->externals_priorities['js']['key']));
- $index = $CONFIG->externals_priorities['js']['key'];
- $this->assertIdentical('http://test1.com', $CONFIG->externals['js'][$index]->url);
+ $this->assertTrue(isset($CONFIG->externals_map['js']['key']));
+
+ $item = $CONFIG->externals_map['js']['key'];
+ $this->assertTrue($CONFIG->externals['js']->contains($item));
+
+ $priority = $CONFIG->externals['js']->getPriority($item);
+ $this->assertTrue($priority !== false);
+
+ $item = $CONFIG->externals['js']->getElement($priority);
+ $this->assertIdentical('http://test1.com', $item->url);
// send a bad url
$result = @elgg_register_js('bad');
@@ -121,13 +128,20 @@ class ElggCoreHelpersTest extends ElggCoreUnitTest {
*/
public function testElggRegisterCSS() {
global $CONFIG;
-
+
// specify name
$result = elgg_register_css('key', 'http://test1.com');
$this->assertTrue($result);
- $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);
+ $this->assertTrue(isset($CONFIG->externals_map['css']['key']));
+
+ $item = $CONFIG->externals_map['css']['key'];
+ $this->assertTrue($CONFIG->externals['css']->contains($item));
+
+ $priority = $CONFIG->externals['css']->getPriority($item);
+ $this->assertTrue($priority !== false);
+
+ $item = $CONFIG->externals['css']->getElement($priority);
+ $this->assertIdentical('http://test1.com', $item->url);
}
/**
@@ -139,6 +153,7 @@ class ElggCoreHelpersTest extends ElggCoreUnitTest {
$base = trim(elgg_get_site_url(), "/");
$urls = array('id1' => "$base/urla", 'id2' => "$base/urlb", 'id3' => "$base/urlc");
+
foreach ($urls as $id => $url) {
elgg_register_js($id, $url);
}
@@ -148,26 +163,33 @@ class ElggCoreHelpersTest extends ElggCoreUnitTest {
$js = $CONFIG->externals['js'];
$elements = $js->getElements();
- $this->assertFalse(isset($CONFIG->externals_priorities['js']['id1']));
+ $this->assertFalse(isset($CONFIG->externals_map['js']['id1']));
+
foreach ($elements as $element) {
$this->assertFalse($element->name == 'id1');
}
$result = elgg_unregister_js('id1');
$this->assertFalse($result);
+
$result = elgg_unregister_js('', 'does_not_exist');
$this->assertFalse($result);
$result = elgg_unregister_js('id2');
$elements = $js->getElements();
- $this->assertFalse(isset($CONFIG->externals_priorities['js']['id2']));
+
+ $this->assertFalse(isset($CONFIG->externals_map['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);
+ $this->assertTrue(isset($CONFIG->externals_map['js']['id3']));
+
+ $priority = $CONFIG->externals['js']->getPriority($CONFIG->externals_map['js']['id3']);
+ $this->assertTrue($priority !== false);
+
+ $item = $CONFIG->externals['js']->getElement($priority);
+ $this->assertIdentical($urls['id3'], $item->url);
}
/**
@@ -180,6 +202,7 @@ class ElggCoreHelpersTest extends ElggCoreUnitTest {
elgg_load_js('key');
$result = elgg_register_js('key', 'http://test1.com', 'footer');
$this->assertTrue($result);
+
$js_urls = elgg_get_loaded_js('footer');
$this->assertIdentical(array('http://test1.com'), $js_urls);
}
@@ -192,7 +215,12 @@ class ElggCoreHelpersTest extends ElggCoreUnitTest {
$base = trim(elgg_get_site_url(), "/");
- $urls = array('id1' => "$base/urla", 'id2' => "$base/urlb", 'id3' => "$base/urlc");
+ $urls = array(
+ 'id1' => "$base/urla",
+ 'id2' => "$base/urlb",
+ 'id3' => "$base/urlc"
+ );
+
foreach ($urls as $id => $url) {
elgg_register_js($id, $url);
elgg_load_js($id);
@@ -315,6 +343,28 @@ class ElggCoreHelpersTest extends ElggCoreUnitTest {
$this->assertIdentical($elements[2], $test_elements[2]);
}
+ public function testElggPriorityListMove() {
+ $pl = new ElggPriorityList();
+
+ $elements = array(
+ -5 => 'Test Element -5',
+ 0 => 'Test Element 0',
+ 5 => 'Test Element 5',
+ );
+
+ foreach ($elements as $priority => $element) {
+ $pl->add($element, $priority);
+ }
+
+ $this->assertTrue($pl->move($elements[-5], 10));
+
+ // check it's at the new place
+ $this->assertIdentical($elements[-5], $pl->getElement(10));
+
+ // check it's not at the old
+ $this->assertFalse($pl->getElement(-5));
+ }
+
public function testElggPriorityListConstructor() {
$elements = array(
10 => 'Test Element 10',
@@ -358,6 +408,25 @@ class ElggCoreHelpersTest extends ElggCoreUnitTest {
$this->assertIdentical(2, $pl->getPriority($elements[2]));
}
+ public function testElggPriorityListGetElement() {
+ $pl = new ElggPriorityList();
+ $priorities = array();
+
+ $elements = array(
+ 'Test element 0',
+ 'Test element 1',
+ 'Test element 2',
+ );
+
+ foreach ($elements as $element) {
+ $priorities[] = $pl->add($element);
+ }
+
+ $this->assertIdentical($elements[0], $pl->getElement($priorities[0]));
+ $this->assertIdentical($elements[1], $pl->getElement($priorities[1]));
+ $this->assertIdentical($elements[2], $pl->getElement($priorities[2]));
+ }
+
public function testElggPriorityListPriorityCollision() {
$pl = new ElggPriorityList();
@@ -378,31 +447,6 @@ class ElggCoreHelpersTest extends ElggCoreUnitTest {
$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',
@@ -422,13 +466,13 @@ class ElggCoreHelpersTest extends ElggCoreUnitTest {
$this->assertEqual(0, count($pl));
- $pl[] = 'Test element 0';
+ $pl->add('Test element 0');
$this->assertEqual(1, count($pl));
- $pl[] = 'Test element 1';
+ $pl->add('Test element 1');
$this->assertEqual(2, count($pl));
- $pl[] = 'Test element 2';
+ $pl->add('Test element 2');
$this->assertEqual(3, count($pl));
}
@@ -461,43 +505,4 @@ class ElggCoreHelpersTest extends ElggCoreUnitTest {
$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