UpdatePostUpdateTest.php 2.91 KB
Newer Older
1
2
3
4
5
6
7
8
9
<?php

/**
 * @file
 * Contains \Drupal\system\Tests\Update\UpdatePostUpdateTest.
 */

namespace Drupal\system\Tests\Update;

10
11
use Drupal\Component\Render\FormattableMarkup;

12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/**
 * Tests hook_post_update().
 *
 * @group Update
 */
class UpdatePostUpdateTest extends UpdatePathTestBase {

  /**
   * {@inheritdoc}
   */
  protected function setDatabaseDumpFiles() {
    $this->databaseDumpFiles = [
      __DIR__ . '/../../../tests/fixtures/update/drupal-8.bare.standard.php.gz',
      __DIR__ . '/../../../tests/fixtures/update/drupal-8.update-test-postupdate-enabled.php',
    ];
  }

  /**
   * {@inheritdoc}
   */
  protected function doSelectionTest() {
    parent::doSelectionTest();

    // Ensure that normal and post_update updates are merged together on the
    // selection page.
37
    $this->assertRaw('<ul><li>8001 -   Normal update_N() function. </li><li>First update.</li><li>Second update.</li><li>Test0 update.</li><li>Test1 update.</li><li>Testing batch processing in post updates update.</li></ul>');
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
  }

  /**
   * Tests hook_post_update_NAME().
   */
  public function testPostUpdate() {
    $this->runUpdates();

    $this->assertRaw('<h3>Update first</h3>');
    $this->assertRaw('First update');
    $this->assertRaw('<h3>Update second</h3>');
    $this->assertRaw('Second update');
    $this->assertRaw('<h3>Update test1</h3>');
    $this->assertRaw('Test1 update');
    $this->assertRaw('<h3>Update test0</h3>');
    $this->assertRaw('Test0 update');
54
55
    $this->assertRaw('<h3>Update test_batch</h3>');
    $this->assertRaw('Test post update batches');
56

57
    // Test state value set by each post update.
58
59
60
61
    $updates = [
      'update_test_postupdate_post_update_first',
      'update_test_postupdate_post_update_second',
      'update_test_postupdate_post_update_test0',
62
      'update_test_postupdate_post_update_test1',
63
64
65
      'update_test_postupdate_post_update_test_batch-1',
      'update_test_postupdate_post_update_test_batch-2',
      'update_test_postupdate_post_update_test_batch-3',
66
67
68
    ];
    $this->assertIdentical($updates, \Drupal::state()->get('post_update_test_execution', []));

69
70
71
72
73
74
75
76
77
78
79
80
81
    // Test post_update key value stores contains a list of the update functions
    // that have run.
    $existing_updates = array_count_values(\Drupal::keyValue('post_update')->get('existing_updates'));
    $expected_updates = [
      'update_test_postupdate_post_update_first',
      'update_test_postupdate_post_update_second',
      'update_test_postupdate_post_update_test1',
      'update_test_postupdate_post_update_test0',
      'update_test_postupdate_post_update_test_batch',
    ];
    foreach ($expected_updates as $expected_update) {
      $this->assertEqual($existing_updates[$expected_update], 1, new FormattableMarkup("@expected_update exists in 'existing_updates' key and only appears once.", ['@expected_update' => $expected_update]));
    }
82
83
84
85
86
87

    $this->drupalGet('update.php/selection');
    $this->assertText('No pending updates.');
  }

}