0b22cc3ae4750229e7fd0e4b5a522262e8f598ef
[yaffs-website] / web / core / modules / path / tests / src / Kernel / Migrate / d6 / MigrateUrlAliasTest.php
1 <?php
2
3 namespace Drupal\Tests\path\Kernel\Migrate\d6;
4
5 use Drupal\migrate\Plugin\MigrateIdMapInterface;
6 use Drupal\Core\Database\Database;
7 use Drupal\Tests\migrate_drupal\Kernel\d6\MigrateDrupal6TestBase;
8
9 /**
10  * URL alias migration.
11  *
12  * @group migrate_drupal_6
13  */
14 class MigrateUrlAliasTest extends MigrateDrupal6TestBase {
15
16   /**
17    * {@inheritdoc}
18    */
19   public static $modules = ['language', 'content_translation', 'path', 'menu_ui'];
20
21   /**
22    * {@inheritdoc}
23    */
24   protected function setUp() {
25     parent::setUp();
26     $this->installEntitySchema('node');
27     $this->installConfig(['node']);
28     $this->installSchema('node', ['node_access']);
29     $this->migrateUsers(FALSE);
30     $this->migrateFields();
31
32     $this->executeMigrations([
33       'language',
34       'd6_node_settings',
35       'd6_node',
36       'd6_node_translation',
37       'd6_url_alias',
38     ]);
39   }
40
41   /**
42    * Assert a path.
43    *
44    * @param string $pid
45    *   The path id.
46    * @param array $conditions
47    *   The path conditions.
48    * @param array $path
49    *   The path.
50    */
51   private function assertPath($pid, $conditions, $path) {
52     $this->assertTrue($path, "Path alias for " . $conditions['source'] . " successfully loaded.");
53     $this->assertIdentical($conditions['alias'], $path['alias']);
54     $this->assertIdentical($conditions['langcode'], $path['langcode']);
55     $this->assertIdentical($conditions['source'], $path['source']);
56   }
57
58   /**
59    * Test the url alias migration.
60    */
61   public function testUrlAlias() {
62     $id_map = $this->getMigration('d6_url_alias')->getIdMap();
63     // Test that the field exists.
64     $conditions = [
65       'source' => '/node/1',
66       'alias' => '/alias-one',
67       'langcode' => 'af',
68     ];
69     $path = \Drupal::service('path.alias_storage')->load($conditions);
70     $this->assertPath('1', $conditions, $path);
71     $this->assertIdentical($id_map->lookupDestinationID([$path['pid']]), ['1'], "Test IdMap");
72
73     $conditions = [
74       'source' => '/node/2',
75       'alias' => '/alias-two',
76       'langcode' => 'en',
77     ];
78     $path = \Drupal::service('path.alias_storage')->load($conditions);
79     $this->assertPath('2', $conditions, $path);
80
81     // Test that we can re-import using the UrlAlias destination.
82     Database::getConnection('default', 'migrate')
83       ->update('url_alias')
84       ->fields(['dst' => 'new-url-alias'])
85       ->condition('src', 'node/2')
86       ->execute();
87
88     \Drupal::database()
89       ->update($id_map->mapTableName())
90       ->fields(['source_row_status' => MigrateIdMapInterface::STATUS_NEEDS_UPDATE])
91       ->execute();
92     $migration = $this->getMigration('d6_url_alias');
93     $this->executeMigration($migration);
94
95     $path = \Drupal::service('path.alias_storage')->load(['pid' => $path['pid']]);
96     $conditions['alias'] = '/new-url-alias';
97     $this->assertPath('2', $conditions, $path);
98
99     $conditions = [
100       'source' => '/node/3',
101       'alias' => '/alias-three',
102       'langcode' => 'und',
103     ];
104     $path = \Drupal::service('path.alias_storage')->load($conditions);
105     $this->assertPath('3', $conditions, $path);
106
107     $path = \Drupal::service('path.alias_storage')->load(['alias' => '/source-noslash']);
108     $conditions = [
109       'source' => '/admin',
110       'alias' => '/source-noslash',
111       'langcode' => 'und',
112     ];
113     $this->assertPath('2', $conditions, $path);
114   }
115
116   /**
117    * Test the URL alias migration with translated nodes.
118    */
119   public function testUrlAliasWithTranslatedNodes() {
120     $alias_storage = $this->container->get('path.alias_storage');
121
122     // Alias for the 'The Real McCoy' node in English.
123     $path = $alias_storage->load(['alias' => '/the-real-mccoy']);
124     $this->assertSame('/node/10', $path['source']);
125     $this->assertSame('en', $path['langcode']);
126
127     // Alias for the 'The Real McCoy' French translation,
128     // which should now point to node/10 instead of node/11.
129     $path = $alias_storage->load(['alias' => '/le-vrai-mccoy']);
130     $this->assertSame('/node/10', $path['source']);
131     $this->assertSame('fr', $path['langcode']);
132
133     // Alias for the 'Abantu zulu' node in Zulu.
134     $path = $alias_storage->load(['alias' => '/abantu-zulu']);
135     $this->assertSame('/node/12', $path['source']);
136     $this->assertSame('zu', $path['langcode']);
137
138     // Alias for the 'Abantu zulu' English translation,
139     // which should now point to node/12 instead of node/13.
140     $path = $alias_storage->load(['alias' => '/the-zulu-people']);
141     $this->assertSame('/node/12', $path['source']);
142     $this->assertSame('en', $path['langcode']);
143   }
144
145 }