function TaxonomyTermUpdatePathTest::testConversionToRevisionable

Tests the conversion of taxonomy terms to be revisionable.

See also

taxonomy_post_update_make_taxonomy_term_revisionable()

File

core/modules/taxonomy/tests/src/Functional/Update/TaxonomyTermUpdatePathTest.php, line 157

Class

TaxonomyTermUpdatePathTest
Tests the upgrade path for taxonomy terms.

Namespace

Drupal\Tests\taxonomy\Functional\Update

Code

public function testConversionToRevisionable() {
    // Set the batch size to 1 to test multiple steps.
    drupal_rewrite_settings([
        'settings' => [
            'update_sql_batch_size' => (object) [
                'value' => 1,
                'required' => TRUE,
            ],
        ],
    ]);
    // Check that there are broken terms in the taxonomy tables, initially.
    $this->assertTermName(997, '');
    $this->assertTermName(998, '');
    $this->assertTermName(999, 'tag999-es');
    $this->runUpdates();
    // Check that the update function returned the expected message.
    $this->assertSession()
        ->pageTextContains('Taxonomy terms have been converted to be revisionable. 2 terms with data integrity issues were restored. More details have been logged.');
    // Check the database tables and the field storage definitions.
    $schema = \Drupal::database()->schema();
    $this->assertTrue($schema->tableExists('taxonomy_term_data'));
    $this->assertTrue($schema->tableExists('taxonomy_term_field_data'));
    $this->assertTrue($schema->tableExists('taxonomy_term_revision'));
    $this->assertTrue($schema->tableExists('taxonomy_term_field_revision'));
    $field_storage_definitions = \Drupal::service('entity.last_installed_schema.repository')->getLastInstalledFieldStorageDefinitions('taxonomy_term');
    $this->assertTrue($field_storage_definitions['langcode']->isRevisionable());
    $this->assertTrue($field_storage_definitions['name']->isRevisionable());
    $this->assertTrue($field_storage_definitions['description']->isRevisionable());
    $this->assertTrue($field_storage_definitions['changed']->isRevisionable());
    // Log in as user 1.
    $account = User::load(1);
    $account->passRaw = 'drupal';
    $this->drupalLogin($account);
    // Make sure our vocabulary exists.
    $this->drupalGet('admin/structure/taxonomy/manage/test_vocabulary/overview');
    // Make sure our terms exist.
    $assert_session = $this->assertSession();
    $assert_session->pageTextContains('Test root term');
    $assert_session->pageTextContains('Test child term');
    $this->drupalGet('taxonomy/term/3');
    $assert_session->statusCodeEquals('200');
    // Make sure the terms are still translated.
    $this->drupalGet('taxonomy/term/2/translations');
    $assert_session->linkExists('Test root term - Spanish');
    $storage = \Drupal::entityTypeManager()->getStorage('taxonomy_term');
    // Check that taxonomy terms can be created, saved and then loaded.
    
    /** @var \Drupal\taxonomy\TermInterface $term */
    $term = $storage->create([
        'name' => 'Test term',
        'vid' => 'article',
        'revision_log_message' => 'Initial revision.',
    ]);
    $term->save();
    $storage->resetCache();
    $term = $storage->loadRevision($term->getRevisionId());
    $this->assertEquals('Test term', $term->label());
    $this->assertEquals('article', $term->bundle());
    $this->assertEquals('Initial revision.', $term->getRevisionLogMessage());
    $this->assertTrue($term->isPublished());
    // Check that two terms were restored and one was ignored. The latter cannot
    // be manually restored, since we would end up with two data table records
    // having "default_langcode" equalling 1, which would not make sense.
    $this->assertTermName(997, 'tag997');
    $this->assertTermName(998, 'tag998');
    $this->assertTermName(999, 'tag999-es');
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.