views_plugin_style_mapping.test

Definition of ViewsPluginStyleMappingTest.

File

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