Projet

Général

Profil

Paste
Télécharger (5,12 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / link / tests / link.entity_token.test @ 39a181a4

1
<?php
2

    
3
/**
4
 * @file
5
 * Contains simpletests making sure entity_token integration works.
6
 */
7

    
8
/**
9
 * Testing that tokens can be used in link titles.
10
 */
11
class LinkEntityTokenTest extends LinkBaseTestClass {
12

    
13
  /**
14
   * Get Info.
15
   */
16
  public static function getInfo() {
17
    return array(
18
      'name' => 'Link entity tokens test',
19
      'description' => 'Tests that a link field appears properly in entity tokens',
20
      'group' => 'Link',
21
      'dependencies' => array('token', 'entity', 'entity_token'),
22
    );
23
  }
24

    
25
  /**
26
   * Setup.
27
   */
28
  public function setUp($modules = array()) {
29
    parent::setUp(array('token', 'entity', 'entity_token'));
30
  }
31

    
32
  /**
33
   * Creates a link field, fills it, then uses a loaded node to test tokens.
34
   */
35
  public function testFieldTokenNodeLoaded() {
36
    // Create field.
37
    $settings = array(
38
      'instance[settings][enable_tokens]' => 0,
39
    );
40
    $field_name = $this->createLinkField('page',
41
      $settings);
42

    
43
    // Create page form.
44
    $this->drupalGet('node/add/page');
45
    // $field_name = 'field_' . $name;.
46
    $this->assertField($field_name . '[und][0][title]', 'Title found');
47
    $this->assertField($field_name . '[und][0][url]', 'URL found');
48

    
49
    $token_url_tests = array(
50
      1 => array(
51
        'href' => 'http://example.com/' . $this->randomName(),
52
        'label' => $this->randomName(),
53
      ),
54
      2 => array(
55
        'href' => 'http://example.com/' . $this->randomName() . '?property=value',
56
        'label' => $this->randomName(),
57
      ),
58
      3 => array(
59
        'href' => 'http://example.com/' . $this->randomName() . '#position',
60
        'label' => $this->randomName(),
61
      ),
62
      4 => array(
63
        'href' => 'http://example.com/' . $this->randomName() . '#lower?property=value2',
64
        'label' => $this->randomName(),
65
      ),
66
    );
67
    // @codingStandardsIgnoreLine
68
    // $this->assert('pass', '<pre>' . print_r($token_url_tests, TRUE) . '<pre>');.
69
    foreach ($token_url_tests as &$input) {
70
      $this->drupalGet('node/add/page');
71

    
72
      $edit = array(
73
        'title' => $input['label'],
74
        $field_name . '[und][0][title]' => $input['label'],
75
        $field_name . '[und][0][url]' => $input['href'],
76
      );
77
      $this->drupalPost(NULL, $edit, t('Save'));
78
      $url = $this->getUrl();
79
      $input['url'] = $url;
80
    }
81

    
82
    // Change to anonymous user.
83
    $this->drupalLogout();
84

    
85
    foreach ($token_url_tests as $index => $input2) {
86
      $node = node_load($index);
87
      $this->assertNotEqual(NULL, $node, "Do we have a node?");
88
      $this->assertEqual($node->nid, $index, "Test that we have a node.");
89
      $token_name = '[node:' . str_replace('_', '-', $field_name) . ':url]';
90
      $assert_data = token_replace($token_name,
91
        array('node' => $node));
92
      $this->assertEqual($input2['href'], $assert_data, "Test that the url token has been set to " . $input2['href'] . ' - ' . $assert_data);
93
    }
94
  }
95

    
96
  /**
97
   * Field Token Node Viewed.
98
   *
99
   * Creates a link field, fills it, then uses a loaded and node_view'd node to
100
   * test tokens.
101
   */
102
  public function testFieldTokenNodeViewed() {
103
    // Create field.
104
    $settings = array(
105
      'instance[settings][enable_tokens]' => 0,
106
    );
107
    $field_name = $this->createLinkField('page',
108
      $settings);
109

    
110
    // Create page form.
111
    $this->drupalGet('node/add/page');
112
    // $field_name = 'field_' . $name;.
113
    $this->assertField($field_name . '[und][0][title]', 'Title found');
114
    $this->assertField($field_name . '[und][0][url]', 'URL found');
115

    
116
    $token_url_tests = array(
117
      1 => array(
118
        'href' => 'http://example.com/' . $this->randomName(),
119
        'label' => $this->randomName(),
120
      ),
121
      2 => array(
122
        'href' => 'http://example.com/' . $this->randomName() . '?property=value',
123
        'label' => $this->randomName(),
124
      ),
125
      3 => array(
126
        'href' => 'http://example.com/' . $this->randomName() . '#position',
127
        'label' => $this->randomName(),
128
      ),
129
      4 => array(
130
        'href' => 'http://example.com/' . $this->randomName() . '#lower?property=value2',
131
        'label' => $this->randomName(),
132
      ),
133
    );
134

    
135
    //@codingStandardsIgnoreLine
136
    // $this->assert('pass', '<pre>' . print_r($token_url_tests, TRUE) . '<pre>');.
137
    foreach ($token_url_tests as &$input) {
138
      $this->drupalGet('node/add/page');
139

    
140
      $edit = array(
141
        'title' => $input['label'],
142
        $field_name . '[und][0][title]' => $input['label'],
143
        $field_name . '[und][0][url]' => $input['href'],
144
      );
145
      $this->drupalPost(NULL, $edit, t('Save'));
146
      $url = $this->getUrl();
147
      $input['url'] = $url;
148
    }
149

    
150
    // Change to anonymous user.
151
    $this->drupalLogout();
152

    
153
    foreach ($token_url_tests as $index => $input2) {
154
      $node = node_load($index);
155
      $this->assertNotEqual(NULL, $node, "Do we have a node?");
156
      $this->assertEqual($node->nid, $index, "Test that we have a node.");
157
      $token_name = '[node:' . str_replace('_', '-', $field_name) . ':url]';
158
      $assert_data = token_replace($token_name,
159
        array('node' => $node));
160
      $this->assertEqual($input2['href'], $assert_data, "Test that the url token has been set to " . $input2['href'] . ' - ' . $assert_data);
161
    }
162
  }
163

    
164
}