Further Drupal 8.6.4 changes. Some core files were not committed before a commit...
[yaffs-website] / web / core / modules / taxonomy / tests / src / Functional / Update / TaxonomyTermUpdatePathTest.php
1 <?php
2
3 namespace Drupal\Tests\taxonomy\Functional\Update;
4
5 use Drupal\FunctionalTests\Update\UpdatePathTestBase;
6 use Drupal\user\Entity\User;
7 use Drupal\views\Entity\View;
8
9 /**
10  * Tests the upgrade path for taxonomy terms.
11  *
12  * @group taxonomy
13  * @group Update
14  * @group legacy
15  */
16 class TaxonomyTermUpdatePathTest extends UpdatePathTestBase {
17
18   /**
19    * {@inheritdoc}
20    */
21   protected function setDatabaseDumpFiles() {
22     $this->databaseDumpFiles = [
23       __DIR__ . '/../../../../../system/tests/fixtures/update/drupal-8.filled.standard.php.gz',
24       __DIR__ . '/../../../fixtures/update/drupal-8.views-taxonomy-term-publishing-status-2981887.php',
25     ];
26   }
27
28   /**
29    * Tests the conversion of taxonomy terms to be publishable.
30    *
31    * @see taxonomy_update_8601()
32    */
33   public function testPublishable() {
34     $this->runUpdates();
35
36     // Log in as user 1.
37     $account = User::load(1);
38     $account->passRaw = 'drupal';
39     $this->drupalLogin($account);
40
41     // Make sure our vocabulary exists.
42     $this->drupalGet('admin/structure/taxonomy/manage/test_vocabulary/overview');
43
44     // Make sure our terms exist.
45     $assert_session = $this->assertSession();
46     $assert_session->pageTextContains('Test root term');
47     $assert_session->pageTextContains('Test child term');
48
49     $this->drupalGet('taxonomy/term/3');
50     $assert_session->statusCodeEquals('200');
51
52     // Make sure the terms are still translated.
53     $this->drupalGet('taxonomy/term/2/translations');
54     $assert_session->linkExists('Test root term - Spanish');
55
56     $storage = \Drupal::entityTypeManager()->getStorage('taxonomy_term');
57
58     // Check that the 'content_translation_status' field has been updated
59     // correctly.
60     /** @var \Drupal\taxonomy\TermInterface $term */
61     $term = $storage->load(2);
62     $translation = $term->getTranslation('es');
63     $this->assertTrue($translation->isPublished());
64
65     // Check that taxonomy terms can be created, saved and then loaded.
66     $term = $storage->create([
67       'name' => 'Test term',
68       'vid' => 'tags',
69     ]);
70     $term->save();
71
72     $term = $storage->loadUnchanged($term->id());
73
74     $this->assertEquals('Test term', $term->label());
75     $this->assertEquals('tags', $term->bundle());
76     $this->assertTrue($term->isPublished());
77
78     // Check that the term can be unpublished.
79     $term->setUnpublished();
80     $term->save();
81     $term = $storage->loadUnchanged($term->id());
82     $this->assertFalse($term->isPublished());
83
84     // Test the update does not run when a status field already exists.
85     module_load_install('taxonomy');
86     $this->assertEquals('The publishing status field has <strong>not</strong> been added to taxonomy terms. See <a href="https://www.drupal.org/node/2985366">this page</a> for more information on how to install it.', (string) taxonomy_update_8601());
87     // Test the message can be overridden.
88     \Drupal::state()->set('taxonomy_update_8601_skip_message', 'Another message');
89     $this->assertEquals('Another message', (string) taxonomy_update_8601());
90   }
91
92   /**
93    * Tests handling of the publishing status in taxonomy term views updates.
94    *
95    * @see taxonomy_post_update_handle_publishing_status_addition_in_views()
96    */
97   public function testPublishingStatusUpdateForTaxonomyTermViews() {
98     // Check that the test view was previously using the
99     // 'content_translation_status' field.
100     $config = \Drupal::config('views.view.test_taxonomy_term_view_with_content_translation_status');
101     $display_options = $config->get('display.default.display_options');
102     $this->assertEquals('content_translation_status', $display_options['fields']['content_translation_status']['field']);
103     $this->assertEquals('content_translation_status', $display_options['filters']['content_translation_status']['field']);
104     $this->assertEquals('content_translation_status', $display_options['sorts']['content_translation_status']['field']);
105
106     // Check a test view without any filter.
107     $config = \Drupal::config('views.view.test_taxonomy_term_view_without_content_translation_status');
108     $display_options = $config->get('display.default.display_options');
109     $this->assertEmpty($display_options['filters']);
110
111     $this->runUpdates();
112
113     // Check that a view which had a field, filter and a sort on the
114     // 'content_translation_status' field has been updated to use the new
115     // 'status' field.
116     $view = View::load('test_taxonomy_term_view_with_content_translation_status');
117     foreach ($view->get('display') as $display) {
118       $this->assertEquals('status', $display['display_options']['fields']['content_translation_status']['field']);
119       $this->assertEquals('status', $display['display_options']['sorts']['content_translation_status']['field']);
120       $this->assertEquals('status', $display['display_options']['filters']['content_translation_status']['field']);
121     }
122
123     // Check that a view without any filters has been updated to include a
124     // filter for the 'status' field.
125     $view = View::load('test_taxonomy_term_view_without_content_translation_status');
126     foreach ($view->get('display') as $display) {
127       $this->assertNotEmpty($display['display_options']['filters']);
128       $this->assertEquals('status', $display['display_options']['filters']['status']['field']);
129     }
130   }
131
132   /**
133    * {@inheritdoc}
134    */
135   protected function replaceUser1() {
136     // Do not replace the user from our dump.
137   }
138
139 }