You are here

function UpdateApiEntityDefinitionUpdateTest::testStatusReport in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/system/src/Tests/Entity/Update/UpdateApiEntityDefinitionUpdateTest.php \Drupal\system\Tests\Entity\Update\UpdateApiEntityDefinitionUpdateTest::testStatusReport()

Tests that entity updates are correctly reported in the status report page.

File

core/modules/system/src/Tests/Entity/Update/UpdateApiEntityDefinitionUpdateTest.php, line 139
Contains \Drupal\system\Tests\Entity\Update\UpdateApiEntityDefinitionUpdateTest.

Class

UpdateApiEntityDefinitionUpdateTest
Tests performing entity updates through the Update API.

Namespace

Drupal\system\Tests\Entity\Update

Code

function testStatusReport() {

  // Create a test entity.
  $entity = EntityTest::create([
    'name' => $this
      ->randomString(),
    'user_id' => mt_rand(),
  ]);
  $entity
    ->save();

  // Check that the status report initially displays no error.
  $this
    ->drupalGet('admin/reports/status');
  $this
    ->assertNoRaw('Out of date');
  $this
    ->assertNoRaw('Mismatched entity and/or field definitions');

  // Enable an entity update and check that we have a dedicated status report
  // item.
  $this->container
    ->get('state')
    ->set('entity_test.remove_name_field', TRUE);
  $this
    ->drupalGet('admin/reports/status');
  $this
    ->assertNoRaw('Out of date');
  $this
    ->assertRaw('Mismatched entity and/or field definitions');

  // Enable a db update and check that now the entity update status report
  // item is no longer displayed. We assume an update function will fix the
  // mismatch.
  $this
    ->enableUpdates('entity_test', 'status_report', 8001);
  $this
    ->drupalGet('admin/reports/status');
  $this
    ->assertRaw('Out of date');
  $this
    ->assertRaw('Mismatched entity and/or field definitions');

  // Apply db updates and check that entity updates were not applied.
  $this
    ->applyUpdates();
  $this
    ->drupalGet('admin/reports/status');
  $this
    ->assertNoRaw('Out of date');
  $this
    ->assertRaw('Mismatched entity and/or field definitions');

  // Check that en exception would be triggered when trying to apply them with
  // existing data.
  $message = 'Entity updates cannot run if entity data exists.';
  try {
    $this->updatesManager
      ->applyUpdates();
    $this
      ->fail($message);
  } catch (FieldStorageDefinitionUpdateForbiddenException $e) {
    $this
      ->pass($message);
  }

  // Check the status report is the same after trying to apply updates.
  $this
    ->drupalGet('admin/reports/status');
  $this
    ->assertNoRaw('Out of date');
  $this
    ->assertRaw('Mismatched entity and/or field definitions');

  // Delete entity data, enable a new update, run updates again and check that
  // entity updates were not applied even when no data exists.
  $entity
    ->delete();
  $this
    ->enableUpdates('entity_test', 'status_report', 8002);
  $this
    ->applyUpdates();
  $this
    ->drupalGet('admin/reports/status');
  $this
    ->assertNoRaw('Out of date');
  $this
    ->assertRaw('Mismatched entity and/or field definitions');
}