Commit 25627b05 authored by alexpott's avatar alexpott

Issue #2302165 by cilefen | kgoel: Only allow loading by plugin definition properties.

parent 8170deca
......@@ -635,11 +635,13 @@ protected function prepareLink(array $link, $intersect = FALSE) {
* {@inheritdoc}
*/
public function loadByProperties(array $properties) {
// @todo Only allow loading by plugin definition properties.
https://www.drupal.org/node/2302165
$query = $this->connection->select($this->table, $this->options);
$query->fields($this->table, $this->definitionFields());
foreach ($properties as $name => $value) {
if (!in_array($name, $this->definitionFields())) {
$fields = implode(', ', $this->definitionFields());
throw new \InvalidArgumentException(String::format('An invalid property name, @name was specified. Allowed property names are: @fields.', array('@name' => $name, '@fields' => $fields)));
}
$query->condition($name, $value);
}
$loaded = $this->safeExecuteSelect($query)->fetchAllAssoc('id', \PDO::FETCH_ASSOC);
......
......@@ -71,6 +71,9 @@ public function loadMultiple(array $ids);
* @param array $properties
* The properties to filter by.
*
* @throws \InvalidArgumentException
* Thrown if an invalid property name is specified in $properties.
*
* @return array
* An array of menu link definition arrays.
*/
......
......@@ -299,6 +299,29 @@ public function testSubtreeHeight() {
$this->assertEqual($this->treeStorage->getSubtreeHeight($child4->getPluginId()), 1);
}
/**
* Tests MenuTreeStorage::loadByProperties().
*/
public function testLoadByProperties() {
$properties = array('foo' => 'bar');
$msg = 'An invalid property name throws an exception.';
try {
$this->treeStorage->loadByProperties($properties);
$this->fail($msg);
}
catch (\InvalidArgumentException $e) {
$this->assertEqual(
'An invalid property name, foo was specified. Allowed property names are: menu_name, route_name, route_parameters, url, title, title_arguments, title_context, description, parent, weight, options, expanded, hidden, provider, metadata, class, form_class, id.',
$e->getMessage()
);
$this->pass($msg);
}
$this->addMenuLink(1);
$properties = array(array('menu_name' => 'tools'));
$links = $this->treeStorage->loadByProperties($properties);
$this->assertEqual('tools', $links[1]['menu_name']);
}
/**
* Adds a link with the given ID and supply defaults.
*/
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment