1
|
<?php
|
2
|
|
3
|
/**
|
4
|
* Test for _link_sanitize().
|
5
|
*
|
6
|
* @todo Rewrite as a unit test once _link_sanitize() is refactored.
|
7
|
*/
|
8
|
class LinkSanitizeTest extends LinkBaseTestClass {
|
9
|
|
10
|
/**
|
11
|
* Backup of $_GET['q'] to be able to revert it to its original state.
|
12
|
*
|
13
|
* @var string
|
14
|
*/
|
15
|
protected $q;
|
16
|
|
17
|
/**
|
18
|
* Get Info.
|
19
|
*/
|
20
|
public static function getInfo() {
|
21
|
return array(
|
22
|
'name' => 'Link Sanitation Tests',
|
23
|
'description' => 'Tests the _link_sanitize() function by itself, without invoking the field life cycle.',
|
24
|
'group' => 'Link',
|
25
|
);
|
26
|
}
|
27
|
|
28
|
/**
|
29
|
* {@inheritdoc}
|
30
|
*/
|
31
|
public function setUp(array $modules = array()) {
|
32
|
$modules[] = 'node';
|
33
|
parent::setUp($modules);
|
34
|
$this->q = $_GET['q'];
|
35
|
$_GET['q'] = 'test-path';
|
36
|
}
|
37
|
|
38
|
/**
|
39
|
* {@inheritdoc}
|
40
|
*/
|
41
|
public function tearDown(array $modules = array()) {
|
42
|
$_GET['q'] = $this->q;
|
43
|
parent::tearDown();
|
44
|
}
|
45
|
|
46
|
/**
|
47
|
* Generate parameters for testing _link_sanitize().
|
48
|
*/
|
49
|
protected function generateParams($settings = array()) {
|
50
|
$field = array();
|
51
|
$instance['entity_type'] = 'node';
|
52
|
$defaults['attributes'] = array();
|
53
|
$defaults['display']['url_cutoff'] = 20;
|
54
|
$defaults['title'] = 'Test title';
|
55
|
$settings += $defaults;
|
56
|
$settings['display'] += $defaults['display'];
|
57
|
$instance['settings'] = $settings;
|
58
|
$entity = NULL;
|
59
|
return array($field, $instance, $entity);
|
60
|
}
|
61
|
|
62
|
/**
|
63
|
* Test that relative hash URLs are returned as is.
|
64
|
*/
|
65
|
public function testRelativeHash() {
|
66
|
$item['url'] = '#hash-only';
|
67
|
list($field, $instance, $entity) = $this->generateParams();
|
68
|
_link_sanitize($item, NULL, $field, $instance, $entity);
|
69
|
$this->assertEqual('#hash-only', $item['url']);
|
70
|
}
|
71
|
|
72
|
/**
|
73
|
* Test that relative hash URLs can be turned into absolute URLs.
|
74
|
*/
|
75
|
public function testAbsoluteHash() {
|
76
|
$item['url'] = '#hash-only';
|
77
|
list($field, $instance, $entity) = $this->generateParams(array(
|
78
|
'absolute_url' => TRUE,
|
79
|
));
|
80
|
_link_sanitize($item, NULL, $field, $instance, $entity);
|
81
|
$this->assertEqual('test-path', $item['url']);
|
82
|
$this->assertEqual(NULL, $item['query']);
|
83
|
$this->assertEqual('hash-only', $item['fragment']);
|
84
|
}
|
85
|
|
86
|
/**
|
87
|
* Test that relative query URLs are returned as is.
|
88
|
*/
|
89
|
public function testRelativeQuery() {
|
90
|
$item['url'] = '?query=only';
|
91
|
list($field, $instance, $entity) = $this->generateParams();
|
92
|
_link_sanitize($item, NULL, $field, $instance, $entity);
|
93
|
$this->assertEqual('?query=only', $item['url']);
|
94
|
}
|
95
|
|
96
|
/**
|
97
|
* Test that query URLs can be turned into absolute URLs.
|
98
|
*/
|
99
|
public function testAbsoluteQuery() {
|
100
|
$item['url'] = '?query=only';
|
101
|
list($field, $instance, $entity) = $this->generateParams(array(
|
102
|
'absolute_url' => TRUE,
|
103
|
));
|
104
|
_link_sanitize($item, NULL, $field, $instance, $entity);
|
105
|
$this->assertEqual('test-path', $item['url']);
|
106
|
$this->assertEqual(array('query' => 'only'), $item['query']);
|
107
|
$this->assertEqual(NULL, $item['fragment']);
|
108
|
}
|
109
|
|
110
|
/**
|
111
|
* Test that query URLs can be turned into absolute URLs.
|
112
|
*/
|
113
|
public function testBlankTitle() {
|
114
|
$item['title'] = '';
|
115
|
$item['url'] = 'https://www.drupal.org/';
|
116
|
list($field, $instance, $entity) = $this->generateParams();
|
117
|
// Disable the URL cutoff to confirm the URL.
|
118
|
$instance['settings']['display']['url_cutoff'] = FALSE;
|
119
|
_link_sanitize($item, NULL, $field, $instance, $entity);
|
120
|
$this->verbose('<pre>' . print_r($item, TRUE) . '</pre>');
|
121
|
$this->assertEqual('https://www.drupal.org/', $item['display_url']);
|
122
|
$this->assertEqual('https://www.drupal.org/', $item['url']);
|
123
|
$this->assertEqual(array(), $item['query']);
|
124
|
$this->assertEqual(NULL, $item['fragment']);
|
125
|
}
|
126
|
|
127
|
/**
|
128
|
* Test that query URLs can be turned into absolute URLs.
|
129
|
*/
|
130
|
public function testBlankTitleWithQuery() {
|
131
|
$item['title'] = '';
|
132
|
$item['url'] = 'https://www.drupal.org/?page=42';
|
133
|
list($field, $instance, $entity) = $this->generateParams();
|
134
|
// Disable the URL cutoff to confirm the URL.
|
135
|
$instance['settings']['display']['url_cutoff'] = FALSE;
|
136
|
_link_sanitize($item, NULL, $field, $instance, $entity);
|
137
|
$this->verbose('<pre>' . print_r($item, TRUE) . '</pre>');
|
138
|
$this->assertEqual('https://www.drupal.org/?page=42', $item['display_url']);
|
139
|
$this->assertEqual('https://www.drupal.org/', $item['url']);
|
140
|
$this->assertEqual(array('page' => 42), $item['query']);
|
141
|
$this->assertEqual(NULL, $item['fragment']);
|
142
|
}
|
143
|
|
144
|
/**
|
145
|
* Test that query URLs can be turned into absolute URLs.
|
146
|
*/
|
147
|
public function testBlankTitleWithFragment() {
|
148
|
$item['title'] = '';
|
149
|
$item['url'] = 'https://www.drupal.org/#something';
|
150
|
list($field, $instance, $entity) = $this->generateParams();
|
151
|
// Disable the URL cutoff to confirm the URL.
|
152
|
$instance['settings']['display']['url_cutoff'] = FALSE;
|
153
|
_link_sanitize($item, NULL, $field, $instance, $entity);
|
154
|
$this->verbose('<pre>' . print_r($item, TRUE) . '</pre>');
|
155
|
$this->assertEqual('https://www.drupal.org/#something', $item['display_url']);
|
156
|
$this->assertEqual('https://www.drupal.org/', $item['url']);
|
157
|
$this->assertEqual(array(), $item['query']);
|
158
|
$this->assertEqual('something', $item['fragment']);
|
159
|
}
|
160
|
|
161
|
/**
|
162
|
* Test that query URLs can be turned into absolute URLs.
|
163
|
*/
|
164
|
public function testBlankTitleWithQueryAndFragment() {
|
165
|
$item['title'] = '';
|
166
|
$item['url'] = 'https://www.drupal.org/?page=42#something';
|
167
|
list($field, $instance, $entity) = $this->generateParams();
|
168
|
// Disable the URL cutoff to confirm the URL.
|
169
|
$instance['settings']['display']['url_cutoff'] = FALSE;
|
170
|
_link_sanitize($item, NULL, $field, $instance, $entity);
|
171
|
$this->verbose('<pre>' . print_r($item, TRUE) . '</pre>');
|
172
|
$this->assertEqual('https://www.drupal.org/?page=42#something', $item['display_url']);
|
173
|
$this->assertEqual('https://www.drupal.org/', $item['url']);
|
174
|
$this->assertEqual(array('page' => 42), $item['query']);
|
175
|
$this->assertEqual('something', $item['fragment']);
|
176
|
}
|
177
|
|
178
|
}
|