5 * Definition of ViewsPluginStyleMappingTest.
9 * Tests the default/mapping row style.
11 class ViewsPluginStyleMappingTest extends ViewsPluginStyleTestBase {
13 public static function getInfo() {
15 'name' => 'Style: Mapping',
16 'description' => 'Test mapping style functionality.',
17 'group' => 'Views Plugins',
21 public function setUp() {
24 // Reset the plugin data.
25 views_fetch_plugin_data(NULL, NULL, TRUE);
28 protected function viewsPlugins() {
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,
50 * Overrides ViewsTestCase::getBasicView().
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(
57 'name_field' => 'name',
58 'numeric_field' => array(
61 'title_field' => 'name',
62 'toggle_numeric_field' => TRUE,
63 'toggle_title_field' => TRUE,
66 $view->display['default']->handler->override_option('fields', array(
69 'table' => 'views_test',
71 'relationship' => 'none',
75 'table' => 'views_test',
77 'relationship' => 'none',
81 'table' => 'views_test',
83 'relationship' => 'none',
90 * Verifies that the fields were mapped correctly.
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.');
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.');
104 * Tests the mapping of fields.
110 * The view rendered as HTML.
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();
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.');
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('-');
131 // The expected result is the mapping name and the field value,
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)));
141 return $rendered_output;