diff --git a/core/modules/user/src/Tests/UserDataTest.php b/core/modules/user/src/Tests/UserDataTest.php new file mode 100644 index 0000000000..34b4ed2935 --- /dev/null +++ b/core/modules/user/src/Tests/UserDataTest.php @@ -0,0 +1,98 @@ + $this->getDummyUserData()); + $connection = $this->getDatabase($database_contents); + $user_data = new UserData($connection); + + // 1. Get all data of given module. + $expected = array(); + $actual = $user_data->get('foo_module'); + $this->assertSame($expected, $actual); + + // 2. set just uid. + $user_data->get('foo_module', 1); + + // 3. set just name. + $user_data->get('foo_module', NULL, 'key1'); + + // 4. set both uid and name. + $user_data->get('foo_module', 2, 'key2'); + } + + /** + * Get a fake database connection object for use in tests. + * + * @param array $database_contents + * The database contents faked as an array. Each key is a table name, each + * value is a list of table rows, an associative array of field => value. + * @param array $connection_options + * (optional) The array of connection options for the database. + * @param string $prefix + * (optional) The table prefix on the database. + * + * @return \Drupal\Core\Database\Driver\fake\FakeConnection + * The database connection. + */ + protected function getDatabase(array $database_contents, $connection_options = array(), $prefix = '') { + return new FakeConnection($database_contents, $connection_options, $prefix); + } + + /** + * Dummy data. + * + * @return array + */ + protected function getDummyUserData() { + return array( + array( + 'module' => 'foo_module', + 'uid' => 1, + 'name' => 'key1', + 'value' => 'value1', + 'serialized' => FALSE + ), + array( + 'module' => 'foo_module', + 'uid' => 2, + 'name' => 'key2', + 'value' => 'value2', + 'serialized' => FALSE + ), + array( + 'module' => 'foo_module', + 'uid' => 3, + 'name' => 'key3', + 'value' => 'value3', + 'serialized' => FALSE + ), + array( + 'module' => 'bar_module', + 'uid' => 1, + 'name' => 'key1', + 'value' => 'value1', + 'serialized' => FALSE + ), + ); + } + +}