Projet

Général

Profil

Paste
Télécharger (4,34 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / views / tests / styles / views_plugin_style_mapping.test @ 4003efde

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
  /**
22
   * {@inheritdoc}
23
   */
24
  public function setUp(array $modules = array()) {
25
    parent::setUp($modules);
26

    
27
    // Reset the plugin data.
28
    views_fetch_plugin_data(NULL, NULL, TRUE);
29
  }
30

    
31
  protected function viewsPlugins() {
32
    return array(
33
      'style' => array(
34
        'test_mapping' => array(
35
          'title' => t('Field mapping'),
36
          'help' => t('Maps specific fields to specific purposes.'),
37
          'handler' => 'views_test_plugin_style_test_mapping',
38
          'path' => drupal_get_path('module', 'views_test') . '/test_plugins',
39
          'theme' => 'views_view_mapping_test',
40
          'theme path' => drupal_get_path('module', 'views_test'),
41
          'theme file' => 'views_test.module',
42
          'uses row plugin' => FALSE,
43
          'uses fields' => TRUE,
44
          'uses options' => TRUE,
45
          'uses grouping' => FALSE,
46
          'type' => 'normal',
47
        ),
48
      ),
49
    );
50
  }
51

    
52
  /**
53
   * Overrides ViewsTestCase::getBasicView().
54
   */
55
  protected function getBasicView() {
56
    $view = parent::getBasicView();
57
    $view->display['default']->handler->override_option('style_plugin', 'test_mapping');
58
    $view->display['default']->handler->override_option('style_options', array(
59
      'mapping' => array(
60
        'name_field' => 'name',
61
        'numeric_field' => array(
62
          'age',
63
        ),
64
        'title_field' => 'name',
65
        'toggle_numeric_field' => TRUE,
66
        'toggle_title_field' => TRUE,
67
      ),
68
    ));
69
    $view->display['default']->handler->override_option('fields', array(
70
      'age' => array(
71
        'id' => 'age',
72
        'table' => 'views_test',
73
        'field' => 'age',
74
        'relationship' => 'none',
75
      ),
76
      'name' => array(
77
        'id' => 'name',
78
        'table' => 'views_test',
79
        'field' => 'name',
80
        'relationship' => 'none',
81
      ),
82
      'job' => array(
83
        'id' => 'job',
84
        'table' => 'views_test',
85
        'field' => 'job',
86
        'relationship' => 'none',
87
      ),
88
    ));
89
    return $view;
90
  }
91

    
92
  /**
93
   * Verifies that the fields were mapped correctly.
94
   */
95
  public function testMappedOutput() {
96
    $view = $this->getBasicView();
97
    $output = $this->mappedOutputHelper($view);
98
    $this->assertTrue(strpos($output, 'job') === FALSE, 'The job field is added to the view but not in the mapping.');
99

    
100
    $view = $this->getBasicView();
101
    $view->display['default']->display_options['style_options']['mapping']['name_field'] = 'job';
102
    $output = $this->mappedOutputHelper($view);
103
    $this->assertTrue(strpos($output, 'job') !== FALSE, 'The job field is added to the view and is in the mapping.');
104
  }
105

    
106
  /**
107
   * Tests the mapping of fields.
108
   *
109
   * @param view $view
110
   *   The view to test.
111
   *
112
   * @return string
113
   *   The view rendered as HTML.
114
   */
115
  protected function mappedOutputHelper($view) {
116
    $rendered_output = $view->preview();
117
    $this->storeViewPreview($rendered_output);
118
    $rows = $this->elements->body->div->div->div;
119
    $data_set = $this->dataSet();
120

    
121
    $count = 0;
122
    foreach ($rows as $row) {
123
      $attributes = $row->attributes();
124
      $class = (string) $attributes['class'][0];
125
      $this->assertTrue(strpos($class, 'views-row-mapping-test') !== FALSE, 'Make sure that each row has the correct CSS class.');
126

    
127
      foreach ($row->div as $field) {
128
        // Split up the field-level class, the first part is the mapping name
129
        // and the second is the field ID.
130
        $field_attributes = $field->attributes();
131
        $name = strtok((string) $field_attributes['class'][0], '-');
132
        $field_id = strtok('-');
133

    
134
        // The expected result is the mapping name and the field value,
135
        // separated by ':'.
136
        $expected_result = $name . ':' . $data_set[$count][$field_id];
137
        $actual_result = (string) $field;
138
        $this->assertIdentical($expected_result, $actual_result, format_string('The fields were mapped successfully: %name => %field_id', array('%name' => $name, '%field_id' => $field_id)));
139
      }
140

    
141
      $count++;
142
    }
143

    
144
    return $rendered_output;
145
  }
146

    
147
}