Projet

Général

Profil

Paste
Télécharger (4 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / ctools / tests / page_tokens.test @ 7e72b748

1
<?php
2

    
3
/**
4
 * Tests for different parts of the ctools object caching system.
5
 *
6
 * Needs to be a WebTest because need access to database tables.
7
 */
8
class CtoolsPageTokens extends DrupalWebTestCase {
9

    
10
  /**
11
   * {@inheritDoc}
12
   */
13
  public static function getInfo() {
14
    return array(
15
      'name' => 'Page token processing',
16
      'description' => 'Verify that page tokens can be set and read.',
17
      'group' => 'ctools',
18
      'dependencies' => array('ctools'),
19
    );
20
  }
21

    
22
  /**
23
   * {@inheritDoc}
24
   */
25
  function setUp(array $modules = array()) {
26
    $modules[] = 'ctools';
27
    parent::setUp($modules);
28

    
29
    // Start each test anew.
30
    ctools_reset_page_tokens();
31
  }
32

    
33
  /**
34
   * Test that we can set page tokens.
35
   */
36
  public function testSetPageToken() {
37
    ctools_set_page_token('id', 'variable', 'test');
38

    
39
    $tokens = ctools_set_page_token();
40
    $this->assertTrue(is_array($tokens) && count($tokens) === 1, 'Page tokens no longer empty.');
41
    $this->assertEqual($tokens['id'], array('variable', 'test'), 'Page token has correct value.');
42
  }
43

    
44
  /**
45
   * Test that we can set page tokens.
46
   */
47
  public function testSetVariableToken() {
48
    $string = ctools_set_variable_token('title');
49
    $tokens = ctools_set_page_token();
50

    
51
    $this->assertTrue(is_array($tokens) && count($tokens) === 1, 'Page tokens no longer empty');
52
    $this->assertEqual($tokens[$string], array('variable', 'title'), 'Page tokens no longer empty');
53
  }
54

    
55
  /**
56
   * Test that we can set page tokens.
57
   */
58
  public function testReplaceVariableToken() {
59
    $string = ctools_set_variable_token('title');
60
    $this->assertEqual($string, '<!-- ctools-page-title -->', 'Expected form of token was found');
61

    
62
    $elements = array(
63
      '#type' => 'container',
64
      '#attributes' => array('class' => array('test')),
65
      'title' => '<h2>Title</h2>',
66
      'content' => array(
67
        '#type' => 'markup',
68
        '#markup' => t('This is my test markup'),
69
        '#prefix' => $string,
70
      ),
71
      'link' => array(
72
        '#type' => 'link',
73
        '#title' => t('My link'),
74
        '#href' => 'node/1',
75
      ),
76
    );
77
    $markup = '<div class="test"><!-- ctools-page-title -->This is my test markup<a href="node/1">My link</a></div>';
78

    
79
    $new_markup = ctools_page_token_processing($markup, $elements);
80

    
81
    $this->assertTrue(is_string($new_markup) && strlen($new_markup) > 1, 'Should return string');
82

    
83
    $this->assertTrue(strstr($new_markup, '<h2>'), 'Variable Token Markup should contain h2 element');
84
    $this->assertFalse(strstr($new_markup, 'ctools-page-title'), 'Variable Token Markup should not contain comment element');
85
  }
86

    
87
  /**
88
   * Test that we can set page tokens.
89
   */
90
  public function testReplaceCallbackToken() {
91
    $string = ctools_set_callback_token('title', 'test_ctools_page_callback_token');
92
    $this->assertEqual($string, '<!-- ctools-page-title -->', 'Expected form of token was found');
93

    
94
    $elements = array(
95
      '#type' => 'container',
96
      '#attributes' => array('class' => array('test')),
97
      'content' => array(
98
        '#type' => 'markup',
99
        '#markup' => t('This is my test markup'),
100
        '#prefix' => $string,
101
      ),
102
      'link' => array(
103
        '#type' => 'link',
104
        '#title' => t('My link'),
105
        '#href' => 'node/1',
106
      ),
107
    );
108
    $markup = '<div class="test"><!-- ctools-page-title -->This is my test markup<a href="node/1">My link</a></div>';
109

    
110
    $new_markup = ctools_page_token_processing($markup, $elements);
111

    
112
    $this->assertTrue(is_string($new_markup) && strlen($new_markup) > 1, 'Should return a non-empty string');
113

    
114
    $this->assertTrue(strstr($new_markup, '<h2>'), 'Callback Token Markup should contain h2 element');
115
    $this->assertFalse(strstr($new_markup, 'ctools-page-title'), 'Callback Token Markup should not contain comment element');
116
  }
117

    
118
}
119

    
120
/**
121
 *
122
 * @param $elements
123
 *
124
 * @return string
125
 */
126
function test_ctools_page_callback_token($elements) {
127
  // Check that 'elements' array looks good.
128
  if (isset($elements['content'])) {
129
    return '<h2>Title</h2>';
130
  }
131
  else {
132
    return '<!--bad-->';
133
  }
134
}