summaryrefslogtreecommitdiffstats
path: root/tests/src/Kernel/BasicTest.php
blob: 5cbba7e4506298ca868cdcf9e7bdeb7947d7d223 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
<?php

namespace Drupal\Tests\views_natural_sort\Kernel;

use Drupal\node\Entity\Node;
use Drupal\Tests\views\Kernel\ViewsKernelTestBase;
use Drupal\views\Tests\ViewTestData;
use Drupal\views\Views;

/**
* @group views_natural_sort
*/
class BasicTest extends ViewsKernelTestBase {

  public static $modules = ['node', 'field', 'text', 'user', 'views_natural_sort', 'views_natural_sort_test'];

  public static $testViews = ['views_natural_sort_test'];

  protected function setUp($import_test_views = TRUE) {
    parent::setUp($import_test_views);

    $this->installEntitySchema('node');
    $this->installEntitySchema('user');
    $this->installSchema('views_natural_sort', 'views_natural_sort');
    $this->installConfig(['node', 'field', 'views_natural_sort']);

    ViewTestData::createTestViews(get_class($this), ['views_natural_sort_test']);
  }

  public function testNaturalSortDefaultBeginningWords() {
    $node1 = Node::create([
      'type' => 'views_natural_sort_test_content',
      'title' => 'A Stripped Zebra',
    ]);
    $node1->save();
    $node2 = Node::create([
      'type' => 'views_natural_sort_test_content',
      'title' => 'Oklahoma',
    ]);
    $node2->save();
    $node3 = Node::create([
      'type' => 'views_natural_sort_test_content',
      'title' => 'The King And I',
    ]);
    $node3->save();

    $view = Views::getView('views_natural_sort_test');
    $view->setDisplay();
    $view->preview('default');
    $this->assertIdenticalResultset(
      $view,
      [
        ['title' => 'The King And I'],
        ['title' => 'Oklahoma'],
        ['title' => 'A Stripped Zebra'],
      ],
      ['title' => 'title']
    );
  }

  public function testNaturalSortDefaultWords() {
    $node1 = Node::create([
      'type' => 'views_natural_sort_test_content',
      'title' => 'Red of Purple',
    ]);
    $node1->save();
    $node2 = Node::create([
      'type' => 'views_natural_sort_test_content',
      'title' => 'Red or Green',
    ]);
    $node2->save();
    $node3 = Node::create([
      'type' => 'views_natural_sort_test_content',
      'title' => 'Red and Blue',
    ]);
    $node3->save();

    $view = Views::getView('views_natural_sort_test');
    $view->setDisplay();
    $view->preview('default');
    $this->assertIdenticalResultset(
      $view,
      [
        ['title' => 'Red and Blue'],
        ['title' => 'Red or Green'],
        ['title' => 'Red of Purple'],
      ],
      ['title' => 'title']
    );
  }

  public function testNaturalSortDefaultSymbols() {
    $node1 = Node::create([
      'type' => 'views_natural_sort_test_content',
      'title' => 'A(Z',
    ]);
    $node1->save();
    $node2 = Node::create([
      'type' => 'views_natural_sort_test_content',
      'title' => 'A[B',
    ]);
    $node2->save();
    $node3 = Node::create([
      'type' => 'views_natural_sort_test_content',
      'title' => 'A\\C',
    ]);
    $node3->save();

    $view = Views::getView('views_natural_sort_test');
    $view->setDisplay();
    $view->preview('default');
    $this->assertIdenticalResultset(
      $view,
      [
        ['title' => 'A[B'],
        ['title' => 'A\\C'],
        ['title' => 'A(Z'],
      ],
      ['title' => 'title']
    );
  }

  public function testNaturalSortNumbers() {
    $node1 = Node::create([
      'type' => 'views_natural_sort_test_content',
      'title' => '1 apple',
    ]);
    $node1->save();
    $node2 = Node::create([
      'type' => 'views_natural_sort_test_content',
      'title' => '2 apples',
    ]);
    $node2->save();
    $node3 = Node::create([
      'type' => 'views_natural_sort_test_content',
      'title' => '10 apples',
    ]);
    $node3->save();
    $node4 = Node::create([
      'type' => 'views_natural_sort_test_content',
      'title' => '-1 apples',
    ]);
    $node4->save();
    $node5 = Node::create([
      'type' => 'views_natural_sort_test_content',
      'title' => '-10 apples',
    ]);
    $node5->save();
    $node6 = Node::create([
      'type' => 'views_natural_sort_test_content',
      'title' => '-2 apples',
    ]);
    $node6->save();
    $node7 = Node::create([
      'type' => 'views_natural_sort_test_content',
      'title' => '-3.550 apples',
    ]);
    $node7->save();
    $node8 = Node::create([
      'type' => 'views_natural_sort_test_content',
      'title' => '-3.5501 apples',
    ]);
    $node8->save();
    $node9 = Node::create([
      'type' => 'views_natural_sort_test_content',
      'title' => '3.5501 apples',
    ]);
    $node9->save();
    $node0 = Node::create([
      'type' => 'views_natural_sort_test_content',
      'title' => '3.550 apples',
    ]);
    $node0->save();

    $view = Views::getView('views_natural_sort_test');
    $view->setDisplay();
    $view->preview('default');
    $this->assertIdenticalResultset(
      $view,
      [
        ['title' => '-10 apples'],
        ['title' => '-3.5501 apples'],
        ['title' => '-3.550 apples'],
        ['title' => '-2 apples'],
        ['title' => '-1 apples'],
        ['title' => '1 apple'],
        ['title' => '2 apples'],
        ['title' => '3.550 apples'],
        ['title' => '3.5501 apples'],
        ['title' => '10 apples'],
      ],
      ['title' => 'title']
    );
  }

}