Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / modules / language / tests / src / Kernel / Migrate / d6 / MigrateDefaultLanguageTest.php
1 <?php
2
3 namespace Drupal\Tests\language\Kernel\Migrate\d6;
4
5 use Drupal\language\Entity\ConfigurableLanguage;
6 use Drupal\migrate\Plugin\MigrationInterface;
7 use Drupal\Tests\migrate_drupal\Kernel\d6\MigrateDrupal6TestBase;
8
9 /**
10  * Tests the default language variable migration.
11  *
12  * @group migrate_drupal_6
13  */
14 class MigrateDefaultLanguageTest extends MigrateDrupal6TestBase {
15
16   /**
17    * {@inheritdoc}
18    */
19   public static $modules = ['language'];
20
21   /**
22    * Tests language_default migration with an existing language.
23    */
24   public function testMigrationWithExistingLanguage() {
25     $this->setDefaultLanguage('fr');
26     $this->startCollectingMessages();
27     $this->executeMigrations(['language', 'default_language']);
28
29     // Tests the language is loaded and is the default language.
30     $default_language = ConfigurableLanguage::load('fr');
31     $this->assertNotNull($default_language);
32     $this->assertSame('fr', $this->config('system.site')->get('default_langcode'));
33   }
34
35   /**
36    * Tests language_default migration with a non-existing language.
37    */
38   public function testMigrationWithNonExistentLanguage() {
39     $this->setDefaultLanguage('tv');
40     $this->startCollectingMessages();
41     $this->executeMigrations(['language', 'default_language']);
42
43     // Tests the migration log contains an error message.
44     $messages = $this->migration->getIdMap()->getMessageIterator();
45     $count = 0;
46     foreach ($messages as $message) {
47       $count++;
48       $this->assertSame("The language 'tv' does not exist on this site.", $message->message);
49       $this->assertSame(MigrationInterface::MESSAGE_ERROR, (int) $message->level);
50     }
51     $this->assertSame(1, $count);
52   }
53
54   /**
55    * Tests language_default migration with unset default language variable.
56    */
57   public function testMigrationWithUnsetVariable() {
58     // Delete the language_default variable.
59     $this->sourceDatabase->delete('variable')
60       ->condition('name', 'language_default')
61       ->execute();
62     $this->startCollectingMessages();
63     $this->executeMigrations(['language', 'default_language']);
64
65     $messages = $this->migration->getIdMap()->getMessageIterator()->fetchAll();
66     // Make sure there's no migration exceptions.
67     $this->assertEmpty($messages);
68     // Make sure the default langcode is 'en', as it was the default on D6 & D7.
69     $this->assertSame('en', $this->config('system.site')->get('default_langcode'));
70   }
71
72   /**
73    * Helper method to test the migration.
74    *
75    * @param string $langcode
76    *   The langcode of the default language.
77    */
78   protected function setDefaultLanguage($langcode) {
79     // The default language of the test fixture is English. Change it to
80     // something else before migrating, to be sure that the source site
81     // default language is migrated.
82     $value = 'O:8:"stdClass":11:{s:8:"language";s:2:"' . $langcode . '";s:4:"name";s:6:"French";s:6:"native";s:6:"French";s:9:"direction";s:1:"0";s:7:"enabled";i:1;s:7:"plurals";s:1:"0";s:7:"formula";s:0:"";s:6:"domain";s:0:"";s:6:"prefix";s:0:"";s:6:"weight";s:1:"0";s:10:"javascript";s:0:"";}';
83     $this->sourceDatabase->update('variable')
84       ->fields(['value' => $value])
85       ->condition('name', 'language_default')
86       ->execute();
87   }
88
89 }