summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Rothstein <drothstein@gmail.com>2014-05-06 04:01:04 -0400
committerDavid Rothstein <drothstein@gmail.com>2014-05-06 04:01:04 -0400
commit55b9d543abaec185a0832e73495d27bd05a18021 (patch)
tree60780e57ae5e1daf6bd166d2aeccdd2344a8abe7
parent0c389ed1c55930761dc851dbf3c2fcc9201c85b1 (diff)
downloadbrdo-55b9d543abaec185a0832e73495d27bd05a18021.tar.gz
brdo-55b9d543abaec185a0832e73495d27bd05a18021.tar.bz2
Issue #1525176 by klausi, amateescu, David_Rothstein, dlu, goldenboy, lucascaro, naveko, Georgique, webchick: Using array_diff_assoc() for multilevel arrays in DrupalDefaultEntityController->cacheGet().
-rw-r--r--includes/entity.inc20
-rw-r--r--modules/simpletest/simpletest.info1
-rw-r--r--modules/simpletest/tests/entity_crud.test49
-rw-r--r--modules/taxonomy/taxonomy.test11
4 files changed, 75 insertions, 6 deletions
diff --git a/includes/entity.inc b/includes/entity.inc
index 25f75846c..dc43e730a 100644
--- a/includes/entity.inc
+++ b/includes/entity.inc
@@ -360,9 +360,23 @@ class DrupalDefaultEntityController implements DrupalEntityControllerInterface {
// This ensures the same behavior whether loading from memory or database.
if ($conditions) {
foreach ($entities as $entity) {
- $entity_values = (array) $entity;
- if (array_diff_assoc($conditions, $entity_values)) {
- unset($entities[$entity->{$this->idKey}]);
+ // Iterate over all conditions and compare them to the entity
+ // properties. We cannot use array_diff_assoc() here since the
+ // conditions can be nested arrays, too.
+ foreach ($conditions as $property_name => $condition) {
+ if (is_array($condition)) {
+ // Multiple condition values for one property are treated as OR
+ // operation: only if the value is not at all in the condition array
+ // we remove the entity.
+ if (!in_array($entity->{$property_name}, $condition)) {
+ unset($entities[$entity->{$this->idKey}]);
+ continue 2;
+ }
+ }
+ elseif ($condition != $entity->{$property_name}) {
+ unset($entities[$entity->{$this->idKey}]);
+ continue 2;
+ }
}
}
}
diff --git a/modules/simpletest/simpletest.info b/modules/simpletest/simpletest.info
index 5583c2f82..7b139ba3d 100644
--- a/modules/simpletest/simpletest.info
+++ b/modules/simpletest/simpletest.info
@@ -15,6 +15,7 @@ files[] = tests/bootstrap.test
files[] = tests/cache.test
files[] = tests/common.test
files[] = tests/database_test.test
+files[] = tests/entity_crud.test
files[] = tests/entity_crud_hook_test.test
files[] = tests/entity_query.test
files[] = tests/error.test
diff --git a/modules/simpletest/tests/entity_crud.test b/modules/simpletest/tests/entity_crud.test
new file mode 100644
index 000000000..be1597790
--- /dev/null
+++ b/modules/simpletest/tests/entity_crud.test
@@ -0,0 +1,49 @@
+<?php
+
+/**
+ * @file
+ * Tests for the Entity CRUD API.
+ */
+
+/**
+ * Tests the entity_load() function.
+ */
+class EntityLoadTestCase extends DrupalWebTestCase {
+ protected $profile = 'testing';
+
+ public static function getInfo() {
+ return array(
+ 'name' => 'Entity loading',
+ 'description' => 'Tests the entity_load() function.',
+ 'group' => 'Entity API',
+ );
+ }
+
+ /**
+ * Tests the functionality for loading entities matching certain conditions.
+ */
+ public function testEntityLoadConditions() {
+ // Create a few nodes. One of them is given an edge-case title of "Array",
+ // because loading entities by an array of conditions is subject to PHP
+ // array-to-string conversion issues and we want to test those.
+ $node_1 = $this->drupalCreateNode(array('title' => 'Array'));
+ $node_2 = $this->drupalCreateNode(array('title' => 'Node 2'));
+ $node_3 = $this->drupalCreateNode(array('title' => 'Node 3'));
+
+ // Load all entities so that they are statically cached.
+ $all_nodes = entity_load('node', FALSE);
+
+ // Check that the first node can be loaded by title.
+ $nodes_loaded = entity_load('node', FALSE, array('title' => 'Array'));
+ $this->assertEqual(array_keys($nodes_loaded), array($node_1->nid));
+
+ // Check that the second and third nodes can be loaded by title using an
+ // array of conditions, and that the first node is not loaded from the
+ // cache along with them.
+ $nodes_loaded = entity_load('node', FALSE, array('title' => array('Node 2', 'Node 3')));
+ ksort($nodes_loaded);
+ $this->assertEqual(array_keys($nodes_loaded), array($node_2->nid, $node_3->nid));
+ $this->assertIdentical($nodes_loaded[$node_2->nid], $all_nodes[$node_2->nid], 'Loaded node 2 is identical to cached node.');
+ $this->assertIdentical($nodes_loaded[$node_3->nid], $all_nodes[$node_3->nid], 'Loaded node 3 is identical to cached node.');
+ }
+}
diff --git a/modules/taxonomy/taxonomy.test b/modules/taxonomy/taxonomy.test
index 665f9aebb..fdf354b7c 100644
--- a/modules/taxonomy/taxonomy.test
+++ b/modules/taxonomy/taxonomy.test
@@ -690,15 +690,20 @@ class TaxonomyTermTestCase extends TaxonomyWebTestCase {
$term_objects[$key] = reset($term_objects[$key]);
}
+ // Test editing the node.
+ $node = $this->drupalGetNodeByTitle($edit["title"]);
+ $this->drupalPost('node/' . $node->nid . '/edit', $edit, t('Save'));
+ foreach ($terms as $term) {
+ $this->assertText($term, 'The term was retained after edit and still appears on the node page.');
+ }
+
// Delete term 1.
$this->drupalPost('taxonomy/term/' . $term_objects['term1']->tid . '/edit', array(), t('Delete'));
$this->drupalPost(NULL, NULL, t('Delete'));
$term_names = array($term_objects['term2']->name, $term_objects['term3']->name);
- // Get the node.
- $node = $this->drupalGetNodeByTitle($edit["title"]);
+ // Get the node and verify that the terms that should be there still are.
$this->drupalGet('node/' . $node->nid);
-
foreach ($term_names as $term_name) {
$this->assertText($term_name, format_string('The term %name appears on the node page after one term %deleted was deleted', array('%name' => $term_name, '%deleted' => $term_objects['term1']->name)));
}