1
|
<?php
|
2
|
|
3
|
/**
|
4
|
* @file
|
5
|
* Definition of ViewsPluginStyleMappingTest.
|
6
|
*/
|
7
|
|
8
|
/**
|
9
|
* Tests the default/mapping row style.
|
10
|
*/
|
11
|
class ViewsPluginStyleMappingTest extends ViewsPluginStyleTestBase {
|
12
|
|
13
|
public static function getInfo() {
|
14
|
return array(
|
15
|
'name' => 'Style: Mapping',
|
16
|
'description' => 'Test mapping style functionality.',
|
17
|
'group' => 'Views Plugins',
|
18
|
);
|
19
|
}
|
20
|
|
21
|
public function setUp() {
|
22
|
parent::setUp();
|
23
|
|
24
|
// Reset the plugin data.
|
25
|
views_fetch_plugin_data(NULL, NULL, TRUE);
|
26
|
}
|
27
|
|
28
|
protected function viewsPlugins() {
|
29
|
return array(
|
30
|
'style' => array(
|
31
|
'test_mapping' => array(
|
32
|
'title' => t('Field mapping'),
|
33
|
'help' => t('Maps specific fields to specific purposes.'),
|
34
|
'handler' => 'views_test_plugin_style_test_mapping',
|
35
|
'path' => drupal_get_path('module', 'views_test') . '/test_plugins',
|
36
|
'theme' => 'views_view_mapping_test',
|
37
|
'theme path' => drupal_get_path('module', 'views_test'),
|
38
|
'theme file' => 'views_test.module',
|
39
|
'uses row plugin' => FALSE,
|
40
|
'uses fields' => TRUE,
|
41
|
'uses options' => TRUE,
|
42
|
'uses grouping' => FALSE,
|
43
|
'type' => 'normal',
|
44
|
),
|
45
|
),
|
46
|
);
|
47
|
}
|
48
|
|
49
|
/**
|
50
|
* Overrides ViewsTestCase::getBasicView().
|
51
|
*/
|
52
|
protected function getBasicView() {
|
53
|
$view = parent::getBasicView();
|
54
|
$view->display['default']->handler->override_option('style_plugin', 'test_mapping');
|
55
|
$view->display['default']->handler->override_option('style_options', array(
|
56
|
'mapping' => array(
|
57
|
'name_field' => 'name',
|
58
|
'numeric_field' => array(
|
59
|
'age',
|
60
|
),
|
61
|
'title_field' => 'name',
|
62
|
'toggle_numeric_field' => TRUE,
|
63
|
'toggle_title_field' => TRUE,
|
64
|
),
|
65
|
));
|
66
|
$view->display['default']->handler->override_option('fields', array(
|
67
|
'age' => array(
|
68
|
'id' => 'age',
|
69
|
'table' => 'views_test',
|
70
|
'field' => 'age',
|
71
|
'relationship' => 'none',
|
72
|
),
|
73
|
'name' => array(
|
74
|
'id' => 'name',
|
75
|
'table' => 'views_test',
|
76
|
'field' => 'name',
|
77
|
'relationship' => 'none',
|
78
|
),
|
79
|
'job' => array(
|
80
|
'id' => 'job',
|
81
|
'table' => 'views_test',
|
82
|
'field' => 'job',
|
83
|
'relationship' => 'none',
|
84
|
),
|
85
|
));
|
86
|
return $view;
|
87
|
}
|
88
|
|
89
|
/**
|
90
|
* Verifies that the fields were mapped correctly.
|
91
|
*/
|
92
|
public function testMappedOutput() {
|
93
|
$view = $this->getBasicView();
|
94
|
$output = $this->mappedOutputHelper($view);
|
95
|
$this->assertTrue(strpos($output, 'job') === FALSE, 'The job field is added to the view but not in the mapping.');
|
96
|
|
97
|
$view = $this->getBasicView();
|
98
|
$view->display['default']->display_options['style_options']['mapping']['name_field'] = 'job';
|
99
|
$output = $this->mappedOutputHelper($view);
|
100
|
$this->assertTrue(strpos($output, 'job') !== FALSE, 'The job field is added to the view and is in the mapping.');
|
101
|
}
|
102
|
|
103
|
/**
|
104
|
* Tests the mapping of fields.
|
105
|
*
|
106
|
* @param view $view
|
107
|
* The view to test.
|
108
|
*
|
109
|
* @return string
|
110
|
* The view rendered as HTML.
|
111
|
*/
|
112
|
protected function mappedOutputHelper($view) {
|
113
|
$rendered_output = $view->preview();
|
114
|
$this->storeViewPreview($rendered_output);
|
115
|
$rows = $this->elements->body->div->div->div;
|
116
|
$data_set = $this->dataSet();
|
117
|
|
118
|
$count = 0;
|
119
|
foreach ($rows as $row) {
|
120
|
$attributes = $row->attributes();
|
121
|
$class = (string) $attributes['class'][0];
|
122
|
$this->assertTrue(strpos($class, 'views-row-mapping-test') !== FALSE, 'Make sure that each row has the correct CSS class.');
|
123
|
|
124
|
foreach ($row->div as $field) {
|
125
|
// Split up the field-level class, the first part is the mapping name
|
126
|
// and the second is the field ID.
|
127
|
$field_attributes = $field->attributes();
|
128
|
$name = strtok((string) $field_attributes['class'][0], '-');
|
129
|
$field_id = strtok('-');
|
130
|
|
131
|
// The expected result is the mapping name and the field value,
|
132
|
// separated by ':'.
|
133
|
$expected_result = $name . ':' . $data_set[$count][$field_id];
|
134
|
$actual_result = (string) $field;
|
135
|
$this->assertIdentical($expected_result, $actual_result, format_string('The fields were mapped successfully: %name => %field_id', array('%name' => $name, '%field_id' => $field_id)));
|
136
|
}
|
137
|
|
138
|
$count++;
|
139
|
}
|
140
|
|
141
|
return $rendered_output;
|
142
|
}
|
143
|
|
144
|
}
|