Projet

Général

Profil

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

root / drupal7 / sites / all / modules / ctools / tests / page_tokens.test @ 6e3ce7c2

1
<?php
2

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

    
10
class CtoolsPageTokens extends DrupalWebTestCase {
11

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

    
24
  /**
25
   * {@inheritdoc}
26
   */
27
  public function setUp(array $modules = array()) {
28
    $modules[] = 'ctools';
29
    parent::setUp($modules);
30

    
31
    // Start each test anew.
32
    ctools_reset_page_tokens();
33
  }
34

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

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

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

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

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

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

    
81
    $new_markup = ctools_page_token_processing($markup, $elements);
82

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

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

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

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

    
112
    $new_markup = ctools_page_token_processing($markup, $elements);
113

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

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

    
120
}
121

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