diff --git a/core/modules/system/lib/Drupal/system/Tests/Path/CrudTest.php b/core/modules/system/lib/Drupal/system/Tests/Path/CrudTest.php new file mode 100644 index 0000000..979bc0f --- /dev/null +++ b/core/modules/system/lib/Drupal/system/Tests/Path/CrudTest.php @@ -0,0 +1,93 @@ + t('Path CRUD Tests'), + 'description' => t('Tests the CRUD capabilities of paths.'), + 'group' => t('Path API'), + ); + } + + function __construct($test_id = NULL) { + parent::__construct($test_id); + + $this->fixtures = new UrlAliasFixtures(); + } + + public function tearDown() { + $this->fixtures->dropTables(Database::getConnection()); + + parent::tearDown(); + } + + + function testCRUD() { + //Prepare database table. + $connection = Database::getConnection(); + $this->fixtures->createTables($connection); + + //Create AliasManager and Path object. + $aliasManager = new AliasManager($connection); + $path = new Path($connection, $aliasManager); + + $aliases = $this->fixtures->sampleUrlAliases(); + + //Create a few aliases + foreach ($aliases as $idx => $alias) { + $path->save($alias['source'], $alias['alias'], $alias['langcode']); + + $result = $connection->query('SELECT * FROM {url_alias} WHERE source = :source AND alias= :alias AND langcode = :langcode', array(':source' => $alias['source'], ':alias' => $alias['alias'], ':langcode' => $alias['langcode'])); + $rows = $result->fetchAll(); + + $this->assertEqual(count($rows), 1, format_string('Created an entry for %alias.', array('%alias' => $alias['alias']))); + + //Cache the pid for further tests. + $aliases[$idx]['pid'] = $rows[0]->pid; + } + + //Load a few aliases + foreach ($aliases as $alias) { + $pid = $alias['pid']; + $loadedAlias = $path->load(array('pid' => $pid)); + $this->assertEqual($loadedAlias, $alias, format_string('Loaded the expected path with pid %pid.', array('%pid' => $pid))); + } + + //Update a few aliases + foreach ($aliases as $alias) { + $path->save($alias['source'], $alias['alias'] . '_updated', $alias['langcode'], $alias['pid']); + + $result = $connection->query('SELECT pid FROM {url_alias} WHERE source = :source AND alias= :alias AND langcode = :langcode', array(':source' => $alias['source'], ':alias' => $alias['alias'] . '_updated', ':langcode' => $alias['langcode'])); + $pid = $result->fetchField(); + + $this->assertEqual($pid, $alias['pid'], format_string('Updated entry for pid %pid.', array('%pid' => $pid))); + } + + //Delete a few aliases + foreach ($aliases as $alias) { + $pid = $alias['pid']; + $path->delete(array('pid' => $pid)); + + $result = $connection->query('SELECT * FROM {url_alias} WHERE pid = :pid', array(':pid' => $pid)); + $rows = $result->fetchAll(); + + $this->assertEqual(count($rows), 0, format_string('Deleted entry with pid %pid.', array('%pid' => $pid))); + } + } +} diff --git a/core/modules/system/lib/Drupal/system/Tests/Path/UrlAliasFixtures.php b/core/modules/system/lib/Drupal/system/Tests/Path/UrlAliasFixtures.php new file mode 100644 index 0000000..fecf153 --- /dev/null +++ b/core/modules/system/lib/Drupal/system/Tests/Path/UrlAliasFixtures.php @@ -0,0 +1,91 @@ +urlAliasTableDefinition(); + $schema = $connection->schema(); + + foreach ($tables as $name => $table) { + $schema->dropTable($name); + $schema->createTable($name, $table); + } + } + + /** + * Drop the tables used for the sample data. + * + * @param Drupal\Core\Database\Connection $connection + * The connection to use to drop the tables. + */ + public function dropTables(Connection $connection) { + $tables = $this->urlAliasTableDefinition(); + $schema = $connection->schema(); + + foreach ($tables as $name => $table) { + $schema->dropTable($name); + } + } + + /** + * Returns an array of URL aliases for testing. + * + * @return array of URL alias definitions. + */ + public function sampleUrlAliases() { + return array( + array( + 'source' => 'node/1', + 'alias' => 'alias_for_node_1_en', + 'langcode' => 'en' + ), + array( + 'source' => 'node/2', + 'alias' => 'alias_for_node_2_en', + 'langcode' => 'en' + ), + array( + 'source' => 'node/1', + 'alias' => 'alias_for_node_1_fr', + 'langcode' => 'fr' + ), + array( + 'source' => 'node/1', + 'alias' => 'alias_for_node_1_und', + 'langcode' => 'und' + ) + ); + } + + + /** + * Returns the table definition for the URL alias fixtures. + * + * @return array + * Table definitions. + */ + public function urlAliasTableDefinition() { + $tables = array(); + + module_load_install('system'); + $schema = system_schema(); + + $tables['url_alias'] = $schema['url_alias']; + $tables['key_value'] = $schema['key_value']; + + return $tables; + } +}