Projet

Général

Profil

Révision 4003efde

Ajouté par Assos Assos il y a environ 4 ans

Weekly update of contrib modules

Voir les différences:

drupal7/sites/all/modules/views/tests/views_glossary.test
6 6
 */
7 7

  
8 8
/**
9
 * Tests glossary view ( summary of arguments ).
9
 * Tests the glossary feature.
10 10
 */
11 11
class ViewsGlossaryTestCase extends ViewsSqlTest {
12

  
13
  /**
14
   * {@inheritdoc}
15
   */
12 16
  public static function getInfo() {
13 17
    return array(
14 18
      'name' => 'Glossary Test',
......
17 21
    );
18 22
  }
19 23

  
20
  public function setUp() {
21
    parent::setUp('views');
22
  }
23

  
24 24
  /**
25
   * Tests the default glossary view.
25
   * Tests the glossary feature.
26 26
   */
27 27
  public function testGlossaryView() {
28
    // create a contentype and add some nodes, with a non random title.
28
    // Create a content type and add some nodes, with a non random title.
29 29
    $type = $this->drupalCreateContentType();
30
    $nodes_per_char = array(
30
    $nodes_per_character = array(
31 31
      'd' => 1,
32 32
      'r' => 4,
33 33
      'u' => 10,
......
35 35
      'a' => 3,
36 36
      'l' => 6,
37 37
    );
38
    foreach ($nodes_per_char as $char => $count) {
38
    $nodes = array();
39
    foreach ($nodes_per_character as $character => $count) {
39 40
      $setting = array(
40 41
        'type' => $type->type,
41 42
      );
42 43
      for ($i = 0; $i < $count; $i++) {
43 44
        $node = $setting;
44
        $node['title'] = $char . $this->randomString(3);
45
        $this->drupalCreateNode($node);
45
        $node['title'] = $character . strtolower($this->randomName());
46
        $nodes[$character][] = $this->drupalCreateNode($node);
46 47
      }
47 48
    }
48 49

  
50
    // Sort created nodes the same way the view does, so that we can assert
51
    // correct node ids for each row in the result set later.
52
    foreach ($nodes_per_character as $character => $count) {
53
      usort($nodes[$character], function ($a, $b) {
54
        return strcmp($a->title, $b->title);
55
      });
56
    }
57

  
49 58
    // Execute glossary view.
50 59
    $view = views_get_view('glossary');
51 60
    $view->set_display('attachment');
52 61
    $view->execute_display('attachment');
53 62

  
54
    // Check that the amount of nodes per char.
55
    $result_nodes_per_char = array();
63
    // Check the amount of nodes per character.
56 64
    foreach ($view->result as $item) {
57
      $this->assertEqual($nodes_per_char[$item->title_truncated], $item->num_records);
65
      $this->assertEqual($nodes_per_character[$item->title_truncated], $item->num_records);
66
    }
67
    $view->destroy();
68

  
69
    // Checks that a glossary view with an argument containing one letter
70
    // returns only and all the nodes that start with that letter.
71
    $view = views_get_view('glossary');
72
    $view->init_display();
73
    $this->executeView($view, array('a'));
74
    $result_count = isset($view->result) && is_array($view->result) ? count($view->result) : 0;
75
    $this->assertIdentical($result_count, 3, 'View has 3 results.');
76
    foreach ($view->result as $delta => $item) {
77
      $nid = isset($view->result[$delta]->nid) ? $view->result[$delta]->nid : '0';
78
      $this->assertIdentical($nid, $nodes['a'][$delta]->nid, 'View result ' . (string) (int) $delta . ' has correct node id.');
79
    }
80
    $view->destroy();
81

  
82
    // Checks that a glossary view with an argument containing multiple values
83
    // returns only and all nodes that start with these values.
84
    $view = views_get_view('glossary');
85
    $view->init_display();
86
    $arguments = $view->display_handler->get_option('arguments');
87
    $arguments['title']['break_phrase'] = TRUE;
88
    $view->display_handler->set_option('arguments', $arguments);
89
    $this->executeView($view, array('d+p'));
90
    $expected_result_count = $nodes_per_character['d'] + $nodes_per_character['p'];
91
    $result_count = isset($view->result) && is_array($view->result) ? count($view->result) : 0;
92
    $this->assertIdentical($result_count, 3, 'View has 3 results.');
93
    $nid = isset($view->result[0]->nid) ? $view->result[0]->nid : '0';
94
    $this->assertIdentical($nid, $nodes['d'][0]->nid, 'View result 0 has correct node id.');
95
    $nid = isset($view->result[1]->nid) ? $view->result[1]->nid : '0';
96
    $this->assertIdentical($nid, $nodes['p'][0]->nid, 'View result 1 has correct node id.');
97
    $nid = isset($view->result[2]->nid) ? $view->result[2]->nid : '0';
98
    $this->assertIdentical($nid, $nodes['p'][1]->nid, 'View result 2 has correct node id.');
99
    $view->destroy();
100

  
101
    // Checks that a glossary view with a phrase as an argument does not
102
    // interpret that phrase as multiple values.
103
    $view = views_get_view('glossary');
104
    $view->init_display();
105
    $arguments = $view->display_handler->get_option('arguments');
106
    $view->display_handler->set_option('arguments', $arguments);
107
    $this->executeView($view, array('d+p'));
108
    $result_count = isset($view->result) && is_array($view->result) ? count($view->result) : 1;
109
    $this->assertIdentical($result_count, 0, 'View has zero results.');
110
    $view->destroy();
111

  
112
    // Checks that a glossary view with an argument containing one letter
113
    // excludes all nodes that start with that letter.
114
    $view = views_get_view('glossary');
115
    $view->init_display();
116
    $arguments = $view->display_handler->get_option('arguments');
117
    $arguments['title']['not'] = TRUE;
118
    $view->display_handler->set_option('arguments', $arguments);
119
    $this->executeView($view, array('a'));
120
    $result_count = isset($view->result) && is_array($view->result) ? count($view->result) : 0;
121
    $this->assertIdentical($result_count, 23, 'View has 23 results.');
122
    $character = 'd';
123
    $character_delta = 0;
124
    foreach ($view->result as $delta => $item) {
125
      if ($delta === 1) {
126
        $character = 'l';
127
        $character_delta = 0;
128
      }
129
      elseif ($delta === 7) {
130
        $character = 'p';
131
        $character_delta = 0;
132
      }
133
      elseif ($delta === 9) {
134
        $character = 'r';
135
        $character_delta = 0;
136
      }
137
      elseif ($delta === 13) {
138
        $character = 'u';
139
        $character_delta = 0;
140
      }
141
      $nid = isset($view->result[$delta]->nid) ? $view->result[$delta]->nid : '0';
142
      $this->assertIdentical($nid, $nodes[$character][$character_delta]->nid, 'View result ' . (string) (int) $delta . ' has correct node id.');
143
      $character_delta++;
144
    }
145
    $view->destroy();
146

  
147
    // Checks that a glossary view with an argument containing multiple values
148
    // excludes all nodes that start with these values.
149
    $view = views_get_view('glossary');
150
    $view->init_display();
151
    $arguments = $view->display_handler->get_option('arguments');
152
    $arguments['title']['break_phrase'] = TRUE;
153
    $arguments['title']['not'] = TRUE;
154
    $view->display_handler->set_option('arguments', $arguments);
155
    $this->executeView($view, array('a+p'));
156
    $result_count = isset($view->result) && is_array($view->result) ? count($view->result) : 0;
157
    $this->assertIdentical($result_count, 21, 'View has 21 results.');
158
    $character = 'd';
159
    $character_delta = 0;
160
    foreach ($view->result as $delta => $item) {
161
      if ($delta === 1) {
162
        $character = 'l';
163
        $character_delta = 0;
164
      }
165
      elseif ($delta === 7) {
166
        $character = 'r';
167
        $character_delta = 0;
168
      }
169
      elseif ($delta === 11) {
170
        $character = 'u';
171
        $character_delta = 0;
172
      }
173
      $nid = isset($view->result[$delta]->nid) ? $view->result[$delta]->nid : '0';
174
      $this->assertIdentical($nid, $nodes[$character][$character_delta]->nid, 'View result ' . (string) (int) $delta . ' has correct node id.');
175
      $character_delta++;
58 176
    }
177
    $view->destroy();
59 178
  }
60 179

  
61 180
}

Formats disponibles : Unified diff