Projet

Général

Profil

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

root / drupal7 / sites / all / modules / link / tests / LinkEntityTokenTest.test @ bad4e148

1 7fe061e8 Assos Assos
<?php
2
3
/**
4
 * @file
5 bad4e148 Assos Assos
 * Testing that tokens can be used in link titles.
6 7fe061e8 Assos Assos
 */
7
8
/**
9 39a181a4 Assos Assos
 * Testing that tokens can be used in link titles.
10 7fe061e8 Assos Assos
 */
11
class LinkEntityTokenTest extends LinkBaseTestClass {
12
13 39a181a4 Assos Assos
  /**
14
   * Get Info.
15
   */
16 7fe061e8 Assos Assos
  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 39a181a4 Assos Assos
  /**
26 8e7483ab Assos Assos
   * {@inheritdoc}
27 39a181a4 Assos Assos
   */
28 8e7483ab Assos Assos
  public function setUp(array $modules = array()) {
29
    $modules[] = 'token';
30
    $modules[] = 'entity';
31
    $modules[] = 'entity_token';
32
    parent::setUp($modules);
33 7fe061e8 Assos Assos
  }
34 39a181a4 Assos Assos
35 7fe061e8 Assos Assos
  /**
36
   * Creates a link field, fills it, then uses a loaded node to test tokens.
37
   */
38 39a181a4 Assos Assos
  public function testFieldTokenNodeLoaded() {
39
    // Create field.
40 7fe061e8 Assos Assos
    $settings = array(
41
      'instance[settings][enable_tokens]' => 0,
42
    );
43
    $field_name = $this->createLinkField('page',
44 39a181a4 Assos Assos
      $settings);
45 7fe061e8 Assos Assos
46 39a181a4 Assos Assos
    // Create page form.
47 7fe061e8 Assos Assos
    $this->drupalGet('node/add/page');
48 39a181a4 Assos Assos
    // $field_name = 'field_' . $name;.
49 7fe061e8 Assos Assos
    $this->assertField($field_name . '[und][0][title]', 'Title found');
50
    $this->assertField($field_name . '[und][0][url]', 'URL found');
51
52
    $token_url_tests = array(
53
      1 => array(
54
        'href' => 'http://example.com/' . $this->randomName(),
55
        'label' => $this->randomName(),
56
      ),
57
      2 => array(
58
        'href' => 'http://example.com/' . $this->randomName() . '?property=value',
59
        'label' => $this->randomName(),
60
      ),
61
      3 => array(
62
        'href' => 'http://example.com/' . $this->randomName() . '#position',
63
        'label' => $this->randomName(),
64
      ),
65
      4 => array(
66
        'href' => 'http://example.com/' . $this->randomName() . '#lower?property=value2',
67
        'label' => $this->randomName(),
68
      ),
69
    );
70 39a181a4 Assos Assos
    // @codingStandardsIgnoreLine
71
    // $this->assert('pass', '<pre>' . print_r($token_url_tests, TRUE) . '<pre>');.
72 7fe061e8 Assos Assos
    foreach ($token_url_tests as &$input) {
73
      $this->drupalGet('node/add/page');
74 39a181a4 Assos Assos
75 7fe061e8 Assos Assos
      $edit = array(
76
        'title' => $input['label'],
77
        $field_name . '[und][0][title]' => $input['label'],
78
        $field_name . '[und][0][url]' => $input['href'],
79
      );
80
      $this->drupalPost(NULL, $edit, t('Save'));
81
      $url = $this->getUrl();
82
      $input['url'] = $url;
83
    }
84
85 39a181a4 Assos Assos
    // Change to anonymous user.
86 7fe061e8 Assos Assos
    $this->drupalLogout();
87 39a181a4 Assos Assos
88 7fe061e8 Assos Assos
    foreach ($token_url_tests as $index => $input2) {
89
      $node = node_load($index);
90
      $this->assertNotEqual(NULL, $node, "Do we have a node?");
91
      $this->assertEqual($node->nid, $index, "Test that we have a node.");
92
      $token_name = '[node:' . str_replace('_', '-', $field_name) . ':url]';
93
      $assert_data = token_replace($token_name,
94 39a181a4 Assos Assos
        array('node' => $node));
95 7fe061e8 Assos Assos
      $this->assertEqual($input2['href'], $assert_data, "Test that the url token has been set to " . $input2['href'] . ' - ' . $assert_data);
96
    }
97
  }
98 39a181a4 Assos Assos
99 7fe061e8 Assos Assos
  /**
100 39a181a4 Assos Assos
   * Field Token Node Viewed.
101
   *
102
   * Creates a link field, fills it, then uses a loaded and node_view'd node to
103
   * test tokens.
104 7fe061e8 Assos Assos
   */
105 39a181a4 Assos Assos
  public function testFieldTokenNodeViewed() {
106
    // Create field.
107 7fe061e8 Assos Assos
    $settings = array(
108
      'instance[settings][enable_tokens]' => 0,
109
    );
110
    $field_name = $this->createLinkField('page',
111 39a181a4 Assos Assos
      $settings);
112 7fe061e8 Assos Assos
113 39a181a4 Assos Assos
    // Create page form.
114 7fe061e8 Assos Assos
    $this->drupalGet('node/add/page');
115 39a181a4 Assos Assos
    // $field_name = 'field_' . $name;.
116 7fe061e8 Assos Assos
    $this->assertField($field_name . '[und][0][title]', 'Title found');
117
    $this->assertField($field_name . '[und][0][url]', 'URL found');
118
119
    $token_url_tests = array(
120
      1 => array(
121
        'href' => 'http://example.com/' . $this->randomName(),
122
        'label' => $this->randomName(),
123
      ),
124
      2 => array(
125
        'href' => 'http://example.com/' . $this->randomName() . '?property=value',
126
        'label' => $this->randomName(),
127
      ),
128
      3 => array(
129
        'href' => 'http://example.com/' . $this->randomName() . '#position',
130
        'label' => $this->randomName(),
131
      ),
132
      4 => array(
133
        'href' => 'http://example.com/' . $this->randomName() . '#lower?property=value2',
134
        'label' => $this->randomName(),
135
      ),
136
    );
137
138 39a181a4 Assos Assos
    //@codingStandardsIgnoreLine
139
    // $this->assert('pass', '<pre>' . print_r($token_url_tests, TRUE) . '<pre>');.
140 7fe061e8 Assos Assos
    foreach ($token_url_tests as &$input) {
141
      $this->drupalGet('node/add/page');
142 39a181a4 Assos Assos
143 7fe061e8 Assos Assos
      $edit = array(
144
        'title' => $input['label'],
145
        $field_name . '[und][0][title]' => $input['label'],
146
        $field_name . '[und][0][url]' => $input['href'],
147
      );
148
      $this->drupalPost(NULL, $edit, t('Save'));
149
      $url = $this->getUrl();
150
      $input['url'] = $url;
151
    }
152
153 39a181a4 Assos Assos
    // Change to anonymous user.
154 7fe061e8 Assos Assos
    $this->drupalLogout();
155 39a181a4 Assos Assos
156 7fe061e8 Assos Assos
    foreach ($token_url_tests as $index => $input2) {
157
      $node = node_load($index);
158
      $this->assertNotEqual(NULL, $node, "Do we have a node?");
159
      $this->assertEqual($node->nid, $index, "Test that we have a node.");
160
      $token_name = '[node:' . str_replace('_', '-', $field_name) . ':url]';
161
      $assert_data = token_replace($token_name,
162 39a181a4 Assos Assos
        array('node' => $node));
163 7fe061e8 Assos Assos
      $this->assertEqual($input2['href'], $assert_data, "Test that the url token has been set to " . $input2['href'] . ' - ' . $assert_data);
164
    }
165
  }
166 39a181a4 Assos Assos
167
}