diff --git a/core/modules/user/src/UserDataQuery.php b/core/modules/user/src/UserDataQuery.php index dcbc46a..7b26944 100644 --- a/core/modules/user/src/UserDataQuery.php +++ b/core/modules/user/src/UserDataQuery.php @@ -10,6 +10,34 @@ class UserDataQuery implements UserDataQueryInterface { /** + * The name of the module the data is associated with. + * + * @var string + */ + protected $module; + + /** + * Sets the user account ID the data is associated with. + * + * @var int + */ + protected $uid; + + /** + * The name of the data key. + * + * @var string + */ + protected $name; + + /** + * The value stored. + * + * @var mixed + */ + protected $value; + + /** * The database connection to use. * * @var \Drupal\Core\Database\Query\SelectInterface @@ -29,36 +57,32 @@ public function __construct(SelectInterface $select) { /** * {@inheritdoc} */ - public function module($module) { - $this->select->condition('module', $module); + public function setModule($module) { + $this->module = $module; return $this; } /** * {@inheritdoc} */ - public function uid($uid) { - $this->select->condition('uid', $uid); + public function setUid($uid) { + $this->uid = $uid; return $this; } /** * {@inheritdoc} */ - public function name($name) { - $this->select->condition('name', $name); + public function setName($name) { + $this->name = $name; return $this; } /** * {@inheritdoc} */ - public function value($value) { - if (!is_scalar($value)) { - $value = serialize($value); - $this->select->condition('serialized', 1); - } - $this->select->condition('value', $value); + public function setValue($value) { + $this->value = $value; return $this; } @@ -66,7 +90,27 @@ public function value($value) { * {@inheritdoc} */ public function execute() { - foreach ($this->select->execute() as $row) { + // Create a query with the supplied conditions. + $select = clone $this->select; + if ($this->module) { + $select->condition('module', $this->module); + } + if ($this->uid) { + $select->condition('uid', $this->uid); + } + if ($this->name) { + $select->condition('name', $this->name); + } + if ($this->value) { + if (!is_scalar($this->value)) { + $select->condition('value', serialize($this->value)); + $select->condition('serialized', 1); + } + else { + $select->condition('value', $this->value); + } + } + foreach ($select->execute() as $row) { if ($row->serialized) { $row->value = unserialize($row->value); } diff --git a/core/modules/user/src/UserDataQueryInterface.php b/core/modules/user/src/UserDataQueryInterface.php index 17f6daf..ac314ff 100644 --- a/core/modules/user/src/UserDataQueryInterface.php +++ b/core/modules/user/src/UserDataQueryInterface.php @@ -12,42 +12,45 @@ /** * Sets the module value. * - * @param string $module - * The name of the module the data is associated with. + * @param string|null $module + * The name of the module the data is associated with. Use a NULL value to + * unset an already set module. * * @return $this */ - public function module($module); + public function setModule($module); /** * Sets the user account ID the data is associated with. * - * @param $uid - * The user account ID the data is associated with. + * @param int|null $uid + * The user account ID the data is associated with. Use a NULL value to + * unset an already set user account ID. * * @return $this */ - public function uid($uid); + public function setUid($uid); /** * Sets the name of the data key. * - * @param string $name - * The name of the data key. + * @param string|null $name + * The name of the data key. Use a NULL value to unset an already set data + * key. * * @return $this */ - public function name($name); + public function setName($name); /** * Sets the value stored. * - * @param string $value - * The value stored. + * @param mixed|null $value + * The value stored. Use a NULL value to unset an already set value. * * @return $this */ - public function value($value); + public function setValue($value); /** * Runs the query. diff --git a/core/modules/user/tests/src/Kernel/UserDataQueryTest.php b/core/modules/user/tests/src/Kernel/UserDataQueryTest.php index 0abfc4c..1cefc04 100644 --- a/core/modules/user/tests/src/Kernel/UserDataQueryTest.php +++ b/core/modules/user/tests/src/Kernel/UserDataQueryTest.php @@ -25,10 +25,10 @@ public function setUp() { } /** - * @covers ::module - * @covers ::uid - * @covers ::name - * @covers ::value + * @covers ::setModule + * @covers ::setUid + * @covers ::setName + * @covers ::setValue * @covers ::execute * * @dataProvider userDataQueryProvider @@ -41,16 +41,16 @@ public function testUserDataQuery($contents, $module, $uid, $name, $value, $expe } $user_data_query = $user_data->getUserDataQuery(); if (isset($module)) { - $user_data_query->module($module); + $user_data_query->setModule($module); } if (isset($uid)) { - $user_data_query->uid($uid); + $user_data_query->setUid($uid); } if (isset($name)) { - $user_data_query->name($name); + $user_data_query->setName($name); } if (isset($value)) { - $user_data_query->value($value); + $user_data_query->setValue($value); } $this->assertEquals([(object) $expected], iterator_to_array($user_data_query->execute())); } @@ -99,4 +99,83 @@ public function userDataQueryProvider() { ]; } + /** + * @covers ::setModule + * @covers ::setUid + * @covers ::setName + * @covers ::setValue + * @covers ::execute + */ + public function testUserDataQueryMultipleMethodCalls() { + $contents = [ + [ + 'module' => 'test', + 'uid' => '100', + 'name' => 'foo', + 'value' => 'bar', + ], + [ + 'module' => 'another_test', + 'uid' => '101', + 'name' => 'foo', + 'value' => 'bar', + ], + [ + 'module' => 'test', + 'uid' => '101', + 'name' => 'foo2', + 'value' => 'bar2', + ], + ]; + + /** @var \Drupal\user\UserData $user_data */ + $user_data = \Drupal::service('user.data'); + foreach ($contents as $row) { + $user_data->set($row['module'], $row['uid'], $row['name'], $row['value']); + } + $user_data_query = $user_data->getUserDataQuery(); + $user_data_query->setModule('test'); + $this->assertEquals([(object) $contents[0], (object) $contents[2]], iterator_to_array($user_data_query->execute())); + + $user_data_query->setModule('another_test'); + $this->assertEquals([(object) $contents[1]], iterator_to_array($user_data_query->execute())); + + $user_data_query->setValue('bar'); + $this->assertEquals([(object) $contents[1]], iterator_to_array($user_data_query->execute())); + + // Test unsetting something. + $user_data_query->setModule(NULL); + $this->assertEquals([(object) $contents[0], (object) $contents[1]], iterator_to_array($user_data_query->execute())); + + $user_data_query->setName('foo2'); + $this->assertEquals([], iterator_to_array($user_data_query->execute())); + + $user_data_query->setValue('bar2')->setModule('test'); + $this->assertEquals([(object) $contents[2]], iterator_to_array($user_data_query->execute())); + + $user_data_query->setName('does_not_exist'); + $this->assertEquals([], iterator_to_array($user_data_query->execute())); + + // Test unsetting everything. + $user_data_query->setName(NULL)->setValue(NULL)->setModule(NULL); + $this->assertEquals([(object) $contents[0], (object) $contents[1], (object) $contents[2]], iterator_to_array($user_data_query->execute())); + + $user_data_query->setUid(100); + $this->assertEquals([(object) $contents[0]], iterator_to_array($user_data_query->execute())); + + $user_data_query->setUid(101); + $this->assertEquals([(object) $contents[1], (object) $contents[2]], iterator_to_array($user_data_query->execute())); + } + + /** + * @covers ::execute + */ + public function testEmpty() { + /** @var \Drupal\user\UserData $user_data */ + $user_data = \Drupal::service('user.data'); + $user_data_query = $user_data->getUserDataQuery(); + $user_data_query->setUid(101); + $this->assertSame([], iterator_to_array($user_data_query->execute())); + } + }