Completed
Push — master ( 91a594...a063ee )
by
unknown
46:30 queued 24:23
created

SortClauseTest   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 120
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 120
rs 10
c 0
b 0
f 0
wmc 7
lcom 1
cbo 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
B testFieldSortClause() 0 76 6
A sortingClauseDataProvider() 0 31 1
1
<?php
2
3
/**
4
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
5
 * @license For full copyright and license information view LICENSE file distributed with this source code.
6
 */
7
namespace eZ\Bundle\EzPublishRestBundle\Tests\Functional;
8
9
use eZ\Bundle\EzPublishRestBundle\Tests\Functional\TestCase as RESTFunctionalTestCase;
10
use SimpleXMLElement;
11
12
class SortClauseTest extends RESTFunctionalTestCase
13
{
14
    /**
15
     * @dataProvider sortingClauseDataProvider
16
     *
17
     * @param array $foldersNameToCreate
18
     * @param string $sortClauseXML
19
     * @param array $foldersInExpectedOrder
20
     *
21
     * @throws \Psr\Http\Client\ClientException
22
     */
23
    public function testFieldSortClause(array $foldersNameToCreate, string $sortClauseXML, array $foldersInExpectedOrder)
24
    {
25
        $string = $this->addTestSuffix(__FUNCTION__);
26
        $mainTestFolderContent = $this->createFolder($string, '/api/ezp/v2/content/locations/1/2');
27
28
        $response = $this->sendHttpRequest(
29
            $this->createHttpRequest('GET', $mainTestFolderContent['_href'], '', 'Content+json')
30
        );
31
32
        self::assertHttpResponseCodeEquals($response, 200);
33
34
        $mainFolderContent = json_decode($response->getBody(), true);
35
36
        if (!isset($mainFolderContent['Content']['MainLocation']['_href'])) {
37
            self::fail("Incomplete response (no main location):\n" . $response->getBody() . "\n");
38
        }
39
40
        $mainFolderLocationHref = $mainFolderContent['Content']['MainLocation']['_href'];
41
42
        $locationArray = explode('/', $mainFolderLocationHref);
43
        $mainFolderLocationId = array_pop($locationArray);
44
45
        $foldersNames = [];
46
        foreach ($foldersNameToCreate as $folder) {
47
            $folderContent = $this->createFolder($folder, $mainFolderLocationHref);
48
            $foldersNames[$folder] = $folderContent['Name'];
49
        }
50
51
        $sortedFoldersNames = [];
52
        foreach ($foldersInExpectedOrder as $name) {
53
            $sortedFoldersNames[] = $foldersNames[$name];
54
        }
55
56
        $body = <<< XML
57
<?xml version="1.0" encoding="UTF-8"?>
58
<ViewInput>
59
  <identifier>TestView</identifier>
60
  <LocationQuery>
61
    <Filter>
62
      <ParentLocationIdCriterion>{$mainFolderLocationId}</ParentLocationIdCriterion>
63
    </Filter>
64
    <limit>10</limit>
65
    <offset>0</offset>
66
    <SortClauses>
67
        $sortClauseXML
68
    </SortClauses>
69
  </LocationQuery>
70
</ViewInput>
71
XML;
72
        $request = $this->createHttpRequest(
73
            'POST',
74
            '/api/ezp/v2/views',
75
            'ViewInput+xml; version=1.1',
76
            'View+xml',
77
            $body
78
        );
79
80
        $response = $this->sendHttpRequest(
81
            $request
82
        );
83
84
        self::assertHttpResponseCodeEquals($response, 200);
85
        $xml = new SimpleXMLElement($response->getBody());
86
87
        $searchHits = [];
88
        foreach ($xml->xpath('//Name') as $searchHit) {
89
            $searchHits[] = (string) $searchHit[0];
90
        }
91
92
        $expectedCount = count($foldersInExpectedOrder);
93
        self::assertCount($expectedCount, $searchHits);
94
95
        for ($i = 0; $i <= $expectedCount - 1; ++$i) {
96
            self::assertEquals($sortedFoldersNames[$i], $searchHits[$i]);
97
        }
98
    }
99
100
    public function sortingClauseDataProvider()
101
    {
102
        return [
103
            [
104
                [
105
                    'AAA',
106
                    'BBB',
107
                    'CCC',
108
                ],
109
                '<Field identifier="folder/name">descending</Field>',
110
                [
111
                    'CCC',
112
                    'BBB',
113
                    'AAA',
114
                ],
115
            ],
116
            [
117
                [
118
                    'This',
119
                    'Is Not',
120
                    'Alphabetical',
121
                ],
122
                '<LocationId>descending</LocationId>',
123
                [
124
                    'Alphabetical',
125
                    'Is Not',
126
                    'This',
127
                ],
128
            ],
129
        ];
130
    }
131
}
132