UpdateCoreTest.php 14.4 KB
Newer Older
1 2 3 4
<?php

/**
 * @file
5
 * Contains \Drupal\update\Tests\UpdateCoreTest.
6 7 8 9
 */

namespace Drupal\update\Tests;

10 11
use Drupal\Core\Url;

12
/**
13 14 15 16
 * Tests the Update Manager module through a series of functional tests using
 * mock XML data.
 *
 * @group update
17
 */
18 19
class UpdateCoreTest extends UpdateTestBase {

20 21 22 23 24
  /**
   * Modules to enable.
   *
   * @var array
   */
25
  public static $modules = array('update_test', 'update', 'language');
26

27
  protected function setUp() {
28
    parent::setUp();
29
    $admin_user = $this->drupalCreateUser(array('administer site configuration', 'administer modules', 'administer themes'));
30 31 32
    $this->drupalLogin($admin_user);
  }

33 34 35 36 37 38 39 40 41 42 43 44
  /**
   * Sets the version to x.x.x when no project-specific mapping is defined.
   *
   * @param string $version
   *   The version.
   */
  protected function setSystemInfo($version) {
    $setting = array(
      '#all' => array(
        'version' => $version,
      ),
    );
45
    $this->config('update_test.settings')->set('system_info', $setting)->save();
46
  }
47

48
  /**
49
   * Tests the Update Manager module when no updates are available.
50 51
   */
  function testNoUpdatesAvailable() {
52 53 54 55 56 57 58 59 60
    foreach (array(0, 1) as $minor_version) {
      foreach (array(0, 1) as $patch_version) {
        foreach (array('-alpha1', '-beta1', '') as $extra_version) {
          $this->setSystemInfo("8.$minor_version.$patch_version" . $extra_version);
          $this->refreshUpdateStatus(array('drupal' => "$minor_version.$patch_version" . $extra_version));
          $this->standardTests();
          $this->assertText(t('Up to date'));
          $this->assertNoText(t('Update available'));
          $this->assertNoText(t('Security update required!'));
61
          $this->assertRaw('check.svg', 'Check icon was found.');
62 63 64
        }
      }
    }
65 66 67
  }

  /**
68
   * Tests the Update Manager module when one normal update is available.
69 70
   */
  function testNormalUpdateAvailable() {
71 72 73 74 75 76 77
    $this->setSystemInfo('8.0.0');
    foreach (array(0, 1) as $minor_version) {
      foreach (array('-alpha1', '-beta1', '') as $extra_version) {
        $this->refreshUpdateStatus(array('drupal' => "$minor_version.1" . $extra_version));
        $this->standardTests();
        $this->drupalGet('admin/reports/updates/check');
        $this->assertNoText(t('Security update required!'));
78 79 80
        $this->assertRaw(\Drupal::l("8.$minor_version.1" . $extra_version, Url::fromUri("http://example.com/drupal-8-$minor_version-1$extra_version-release")), 'Link to release appears.');
        $this->assertRaw(\Drupal::l(t('Download'), Url::fromUri("http://example.com/drupal-8-$minor_version-1$extra_version.tar.gz")), 'Link to download appears.');
        $this->assertRaw(\Drupal::l(t('Release notes'), Url::fromUri("http://example.com/drupal-8-$minor_version-1$extra_version-release")), 'Link to release notes appears.');
81 82 83 84 85 86 87 88 89 90

        switch ($minor_version) {
          case 0:
            // Both stable and unstable releases are available.
            // A stable release is the latest.
            if ($extra_version == '') {
              $this->assertNoText(t('Up to date'));
              $this->assertText(t('Update available'));
              $this->assertText(t('Recommended version:'));
              $this->assertNoText(t('Latest version:'));
91
              $this->assertRaw('warning.svg', 'Warning icon was found.');
92 93 94 95 96 97 98 99
            }
            // Only unstable releases are available.
            // An unstable release is the latest.
            else {
              $this->assertText(t('Up to date'));
              $this->assertNoText(t('Update available'));
              $this->assertNoText(t('Recommended version:'));
              $this->assertText(t('Latest version:'));
100
              $this->assertRaw('check.svg', 'Check icon was found.');
101 102 103 104 105 106 107 108 109 110
            }
            break;
          case 1:
            // Both stable and unstable releases are available.
            // A stable release is the latest.
            if ($extra_version == '') {
              $this->assertNoText(t('Up to date'));
              $this->assertText(t('Update available'));
              $this->assertText(t('Recommended version:'));
              $this->assertNoText(t('Latest version:'));
111
              $this->assertRaw('warning.svg', 'Warning icon was found.');
112 113 114 115 116 117 118 119
            }
            // Both stable and unstable releases are available.
            // An unstable release is the latest.
            else {
              $this->assertNoText(t('Up to date'));
              $this->assertText(t('Update available'));
              $this->assertText(t('Recommended version:'));
              $this->assertText(t('Latest version:'));
120
              $this->assertRaw('warning.svg', 'Warning icon was found.');
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
            }
            break;
        }
      }
    }
  }

  /**
   * Tests the Update Manager module when a major update is available.
   */
  function testMajorUpdateAvailable() {
    foreach (array(0, 1) as $minor_version) {
      foreach (array(0, 1) as $patch_version) {
        foreach (array('-alpha1', '-beta1', '') as $extra_version) {
          $this->setSystemInfo("8.$minor_version.$patch_version" . $extra_version);
          $this->refreshUpdateStatus(array('drupal' => '9'));
          $this->standardTests();
          $this->drupalGet('admin/reports/updates/check');
          $this->assertNoText(t('Security update required!'));
140 141 142
          $this->assertRaw(\Drupal::l('9.0.0', Url::fromUri("http://example.com/drupal-9-0-0-release")), 'Link to release appears.');
          $this->assertRaw(\Drupal::l(t('Download'), Url::fromUri("http://example.com/drupal-9-0-0.tar.gz")), 'Link to download appears.');
          $this->assertRaw(\Drupal::l(t('Release notes'), Url::fromUri("http://example.com/drupal-9-0-0-release")), 'Link to release notes appears.');
143 144 145 146
          $this->assertNoText(t('Up to date'));
          $this->assertText(t('Not supported!'));
          $this->assertText(t('Recommended version:'));
          $this->assertNoText(t('Latest version:'));
147
          $this->assertRaw('error.svg', 'Error icon was found.');
148 149 150
        }
      }
    }
151 152 153
  }

  /**
154
   * Tests the Update Manager module when a security update is available.
155 156
   */
  function testSecurityUpdateAvailable() {
157 158 159 160 161 162 163
    foreach (array(0, 1) as $minor_version) {
      $this->setSystemInfo("8.$minor_version.0");
      $this->refreshUpdateStatus(array('drupal' => "$minor_version.2-sec"));
      $this->standardTests();
      $this->assertNoText(t('Up to date'));
      $this->assertNoText(t('Update available'));
      $this->assertText(t('Security update required!'));
164 165 166
      $this->assertRaw(\Drupal::l("8.$minor_version.2", Url::fromUri("http://example.com/drupal-8-$minor_version-2-release")), 'Link to release appears.');
      $this->assertRaw(\Drupal::l(t('Download'), Url::fromUri("http://example.com/drupal-8-$minor_version-2.tar.gz")), 'Link to download appears.');
      $this->assertRaw(\Drupal::l(t('Release notes'), Url::fromUri("http://example.com/drupal-8-$minor_version-2-release")), 'Link to release notes appears.');
167
      $this->assertRaw('error.svg', 'Error icon was found.');
168
    }
169 170 171
  }

  /**
172
   * Ensures proper results where there are date mismatches among modules.
173 174 175 176 177
   */
  function testDatestampMismatch() {
    $system_info = array(
      '#all' => array(
        // We need to think we're running a -dev snapshot to see dates.
178
        'version' => '8.0.0-dev',
179 180 181 182 183 184 185
        'datestamp' => time(),
      ),
      'block' => array(
        // This is 2001-09-09 01:46:40 GMT, so test for "2001-Sep-".
        'datestamp' => '1000000000',
      ),
    );
186
    $this->config('update_test.settings')->set('system_info', $system_info)->save();
187 188 189 190 191 192 193 194
    $this->refreshUpdateStatus(array('drupal' => 'dev'));
    $this->assertNoText(t('2001-Sep-'));
    $this->assertText(t('Up to date'));
    $this->assertNoText(t('Update available'));
    $this->assertNoText(t('Security update required!'));
  }

  /**
195
   * Checks that running cron updates the list of available updates.
196 197
   */
  function testModulePageRunCron() {
198
    $this->setSystemInfo('8.0.0');
199
    $this->config('update.settings')
200
      ->set('fetch.url', Url::fromRoute('update_test.update_test')->setAbsolute()->toString())
201
      ->save();
202
    $this->config('update_test.settings')
203 204
      ->set('xml_map', array('drupal' => '0.0'))
      ->save();
205 206 207 208 209 210 211

    $this->cronRun();
    $this->drupalGet('admin/modules');
    $this->assertNoText(t('No update information available.'));
  }

  /**
212
   * Checks the messages at admin/modules when the site is up to date.
213 214
   */
  function testModulePageUpToDate() {
215
    $this->setSystemInfo('8.0.0');
216
    // Instead of using refreshUpdateStatus(), set these manually.
217
    $this->config('update.settings')
218
      ->set('fetch.url', Url::fromRoute('update_test.update_test')->setAbsolute()->toString())
219
      ->save();
220
    $this->config('update_test.settings')
221 222
      ->set('xml_map', array('drupal' => '0.0'))
      ->save();
223 224 225 226 227 228 229 230 231 232

    $this->drupalGet('admin/reports/updates');
    $this->clickLink(t('Check manually'));
    $this->assertText(t('Checked available update data for one project.'));
    $this->drupalGet('admin/modules');
    $this->assertNoText(t('There are updates available for your version of Drupal.'));
    $this->assertNoText(t('There is a security update available for your version of Drupal.'));
  }

  /**
233
   * Checks the messages at admin/modules when an update is missing.
234 235
   */
  function testModulePageRegularUpdate() {
236
    $this->setSystemInfo('8.0.0');
237
    // Instead of using refreshUpdateStatus(), set these manually.
238
    $this->config('update.settings')
239
      ->set('fetch.url', Url::fromRoute('update_test.update_test')->setAbsolute()->toString())
240
      ->save();
241
    $this->config('update_test.settings')
242 243
      ->set('xml_map', array('drupal' => '0.1'))
      ->save();
244 245 246 247 248 249 250 251 252 253

    $this->drupalGet('admin/reports/updates');
    $this->clickLink(t('Check manually'));
    $this->assertText(t('Checked available update data for one project.'));
    $this->drupalGet('admin/modules');
    $this->assertText(t('There are updates available for your version of Drupal.'));
    $this->assertNoText(t('There is a security update available for your version of Drupal.'));
  }

  /**
254
   * Checks the messages at admin/modules when a security update is missing.
255 256
   */
  function testModulePageSecurityUpdate() {
257
    $this->setSystemInfo('8.0.0');
258
    // Instead of using refreshUpdateStatus(), set these manually.
259
    $this->config('update.settings')
260
      ->set('fetch.url', Url::fromRoute('update_test.update_test')->setAbsolute()->toString())
261
      ->save();
262
    $this->config('update_test.settings')
263 264
      ->set('xml_map', array('drupal' => '0.2-sec'))
      ->save();
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292

    $this->drupalGet('admin/reports/updates');
    $this->clickLink(t('Check manually'));
    $this->assertText(t('Checked available update data for one project.'));
    $this->drupalGet('admin/modules');
    $this->assertNoText(t('There are updates available for your version of Drupal.'));
    $this->assertText(t('There is a security update available for your version of Drupal.'));

    // Make sure admin/appearance warns you you're missing a security update.
    $this->drupalGet('admin/appearance');
    $this->assertNoText(t('There are updates available for your version of Drupal.'));
    $this->assertText(t('There is a security update available for your version of Drupal.'));

    // Make sure duplicate messages don't appear on Update status pages.
    $this->drupalGet('admin/reports/status');
    // We're expecting "There is a security update..." inside the status report
    // itself, but the drupal_set_message() appears as an li so we can prefix
    // with that and search for the raw HTML.
    $this->assertNoRaw('<li>' . t('There is a security update available for your version of Drupal.'));

    $this->drupalGet('admin/reports/updates');
    $this->assertNoText(t('There is a security update available for your version of Drupal.'));

    $this->drupalGet('admin/reports/updates/settings');
    $this->assertNoText(t('There is a security update available for your version of Drupal.'));
  }

  /**
293
   * Tests the Update Manager module when the update server returns 503 errors.
294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
   */
  function testServiceUnavailable() {
    $this->refreshUpdateStatus(array(), '503-error');
    // Ensure that no "Warning: SimpleXMLElement..." parse errors are found.
    $this->assertNoText('SimpleXMLElement');
    $this->assertUniqueText(t('Failed to get available update data for one project.'));
  }

  /**
   * Tests that exactly one fetch task per project is created and not more.
   */
  function testFetchTasks() {
    $projecta = array(
      'name' => 'aaa_update_test',
    );
    $projectb = array(
      'name' => 'bbb_update_test',
    );
312
    $queue = \Drupal::queue('update_fetch_tasks');
313 314 315 316 317 318 319 320 321
    $this->assertEqual($queue->numberOfItems(), 0, 'Queue is empty');
    update_create_fetch_task($projecta);
    $this->assertEqual($queue->numberOfItems(), 1, 'Queue contains one item');
    update_create_fetch_task($projectb);
    $this->assertEqual($queue->numberOfItems(), 2, 'Queue contains two items');
    // Try to add project a again.
    update_create_fetch_task($projecta);
    $this->assertEqual($queue->numberOfItems(), 2, 'Queue still contains two items');

322 323
    // Clear storage and try again.
    update_storage_clear();
324 325 326 327
    update_create_fetch_task($projecta);
    $this->assertEqual($queue->numberOfItems(), 2, 'Queue contains two items');
  }

328 329 330 331
  /**
   * Checks language module in core package at admin/reports/updates.
   */
  function testLanguageModuleUpdate() {
332
    $this->setSystemInfo('8.0.0');
333
    // Instead of using refreshUpdateStatus(), set these manually.
334
    $this->config('update.settings')
335
      ->set('fetch.url', Url::fromRoute('update_test.update_test')->setAbsolute()->toString())
336
      ->save();
337
    $this->config('update_test.settings')
338 339
      ->set('xml_map', array('drupal' => '0.1'))
      ->save();
340 341 342 343 344

    $this->drupalGet('admin/reports/updates');
    $this->assertText(t('Language'));
  }

345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364
  /**
   * Ensures that the local actions appear.
   */
  public function testLocalActions() {
    $admin_user = $this->drupalCreateUser(array('administer site configuration', 'administer modules', 'administer software updates', 'administer themes'));
    $this->drupalLogin($admin_user);

    $this->drupalGet('admin/modules');
    $this->clickLink(t('Install new module'));
    $this->assertUrl('admin/modules/install');

    $this->drupalGet('admin/appearance');
    $this->clickLink(t('Install new theme'));
    $this->assertUrl('admin/theme/install');

    $this->drupalGet('admin/reports/updates');
    $this->clickLink(t('Install new module or theme'));
    $this->assertUrl('admin/reports/updates/install');
  }

365
}