HookExitTest.php 2.85 KB
Newer Older
1
2
3
4
<?php

/**
 * @file
5
 * Contains \Drupal\system\Tests\Bootstrap\HookExitTest.
6
7
8
9
10
11
12
 */

namespace Drupal\system\Tests\Bootstrap;

use Drupal\simpletest\WebTestBase;

/**
13
 * Tests hook_exit().
14
 */
15
class HookExitTest extends WebTestBase {
16

17
18
19
20
21
22
23
  /**
   * Modules to enable.
   *
   * @var array
   */
  public static $modules = array('system_test', 'dblog');

24
25
  public static function getInfo() {
    return array(
26
27
      'name' => 'Exit hook invocation',
      'description' => 'Test that hook_exit() is called correctly.',
28
29
30
31
32
      'group' => 'Bootstrap',
    );
  }

  /**
33
   * Tests calling of hook_exit().
34
   */
35
  function testHookExit() {
36
    // Test with cache disabled. Exit should always fire.
37
    $config = config('system.performance');
38
    $config->set('cache.page.enabled', 0);
39
40
41
42
    $config->save();

    $this->drupalGet('');
    $calls = 1;
43
    $this->assertEqual(db_query('SELECT COUNT(*) FROM {watchdog} WHERE type = :type AND message = :message', array(':type' => 'system_test', ':message' => 'hook_exit'))->fetchField(), $calls, 'hook_exit called with disabled cache.');
44

45
46
    // Test with normal cache. On the first call, exit should fire
    // (since cache is empty), but on the second call it should not be fired.
47
    $config->set('cache.page.enabled', 1);
48
49
50
    $config->save();
    $this->drupalGet('');
    $calls++;
51
52
53
54
    $this->assertEqual(db_query('SELECT COUNT(*) FROM {watchdog} WHERE type = :type AND message = :message', array(':type' => 'system_test', ':message' => 'hook_exit'))->fetchField(), $calls, 'hook_exit called with normal cache and no cached page.');
    $this->assertTrue(cache('page')->get(url('', array('absolute' => TRUE))), 'Page has been cached.');
    $this->drupalGet('');
    $this->assertEqual(db_query('SELECT COUNT(*) FROM {watchdog} WHERE type = :type AND message = :message', array(':type' => 'system_test', ':message' => 'hook_exit'))->fetchField(), $calls, 'hook_exit not called with normal cache and a cached page.');
55

56
    // Test with aggressive cache. Exit should not fire since page is cached.
57
    variable_set('page_cache_invoke_hooks', FALSE);
58
    $this->assertTrue(cache('page')->get(url('', array('absolute' => TRUE))), 'Page has been cached.');
59
    $this->drupalGet('');
60
    $this->assertEqual(db_query('SELECT COUNT(*) FROM {watchdog} WHERE type = :type AND message = :message', array(':type' => 'system_test', ':message' => 'hook_exit'))->fetchField(), $calls, 'hook_exit not called with aggressive cache and a cached page.');
61

62
    // Test with page cache cleared, exit should be called.
63
    $this->assertTrue(db_delete('cache_page')->execute(), 'Page cache cleared.');
64
65
    $this->drupalGet('');
    $calls++;
66
    $this->assertEqual(db_query('SELECT COUNT(*) FROM {watchdog} WHERE type = :type AND message = :message', array(':type' => 'system_test', ':message' => 'hook_exit'))->fetchField(), $calls, 'hook_exit called with aggressive cache and no cached page.');
67
68
  }
}