Completed
Push — 6.13 ( a10142...a8c729 )
by
unknown
23:41
created

ViewTest::createTestContentItems()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 1
dl 0
loc 16
rs 9.7333
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * File containing the Functional\ViewTest class.
5
 *
6
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
7
 * @license For full copyright and license information view LICENSE file distributed with this source code.
8
 */
9
namespace eZ\Bundle\EzPublishRestBundle\Tests\Functional;
10
11
use stdClass;
12
13
class ViewTest extends TestCase
14
{
15
    /** @var string[] */
16
    private static $createdContentRemoteIds = [];
17
18
    /**
19
     * Covers POST /views.
20
     *
21
     * @dataProvider providerForTestViewRequest
22
     *
23
     * @param string $body
24
     * @param string $format
25
     * @param int $expectedResultsCount
26
     * @param \stdClass[] $contentDataList list of items containing name and remoteId properties
27
     */
28
    public function testViewRequest($body, $format, $expectedResultsCount, array $contentDataList)
29
    {
30
        $this->createTestContentItems($contentDataList);
31
32
        // search for Content
33
        $request = $this->createHttpRequest(
34
            'POST',
35
            '/api/ezp/v2/views',
36
            "ViewInput+{$format}",
37
            'View+json'
38
        );
39
        $request->setContent($body);
40
        $response = $this->sendHttpRequest($request);
41
        $responseData = json_decode($response->getContent(), true);
42
43
        if (isset($responseData['ErrorMessage'])) {
44
            self::fail(var_export($responseData, true));
45
        }
46
47
        self::assertEquals($expectedResultsCount, $responseData['View']['Result']['count']);
48
    }
49
50
    /**
51
     * Data provider for testViewRequestWithOrStatement.
52
     *
53
     * @return array
54
     */
55
    public function providerForTestViewRequest()
56
    {
57
        $foo = new stdClass();
58
        $foo->name = uniqid('View test content foo');
59
        $foo->remoteId = md5($foo->name);
60
61
        $bar = new stdClass();
62
        $bar->name = uniqid('View test content bar');
63
        $bar->remoteId = md5($bar->name);
64
65
        return [
66
            [
67
                <<< XML
68
<?xml version="1.0" encoding="UTF-8"?>
69
<ViewInput>
70
  <identifier>TitleView</identifier>
71
  <Query>
72
    <Filter>
73
        <OR>
74
            <ContentRemoteIdCriterion>{$foo->remoteId}</ContentRemoteIdCriterion>
75
            <ContentRemoteIdCriterion>{$bar->remoteId}</ContentRemoteIdCriterion>
76
        </OR>
77
    </Filter>
78
    <limit>10</limit>
79
    <offset>0</offset>
80
  </Query>
81
</ViewInput>
82
XML
83
,
84
                'xml',
85
                2,
86
                [$foo, $bar],
87
            ],
88
            [
89
                <<< JSON
90
{
91
  "ViewInput": {
92
    "identifier": "TitleView",
93
    "Query": {
94
      "Filter": {
95
        "OR": {
96
          "ContentRemoteIdCriterion": [
97
            "{$foo->remoteId}",
98
            "{$bar->remoteId}"
99
          ]
100
        }
101
      },
102
      "limit": "10",
103
      "offset": "0"
104
    }
105
  }
106
}
107
JSON
108
,
109
                'json',
110
                2,
111
                [$foo, $bar],
112
            ],
113
            [
114
                <<< XML
115
<?xml version="1.0" encoding="UTF-8"?>
116
<ViewInput>
117
  <identifier>TitleView</identifier>
118
  <Query>
119
    <Filter>
120
        <AND>
121
            <OR>
122
                <ContentRemoteIdCriterion>{$foo->remoteId}</ContentRemoteIdCriterion>
123
                <ContentRemoteIdCriterion>{$bar->remoteId}</ContentRemoteIdCriterion>
124
            </OR>
125
            <ContentRemoteIdCriterion>{$foo->remoteId}</ContentRemoteIdCriterion>
126
        </AND>
127
    </Filter>
128
    <limit>10</limit>
129
    <offset>0</offset>
130
  </Query>
131
</ViewInput>
132
XML
133
,
134
                'xml',
135
                1,
136
                [$foo, $bar],
137
            ],
138
            [
139
                <<< JSON
140
{
141
  "ViewInput": {
142
    "identifier": "TitleView",
143
    "public": true,
144
    "Query": {
145
      "Filter": {
146
        "OR": [
147
          {
148
            "ContentRemoteIdCriterion": "{$foo->remoteId}"
149
          },
150
          {
151
            "ContentRemoteIdCriterion": "{$bar->remoteId}"
152
          }
153
        ]
154
      },
155
      "FacetBuilders": {},
156
      "SortClauses": {},
157
      "limit": 1000,
158
      "offset": 0
159
    }
160
  }
161
}
162
JSON
163
,
164
                'json',
165
                2,
166
                [$foo, $bar],
167
            ],
168
            [
169
                <<< JSON
170
{
171
  "ViewInput": {
172
    "identifier": "udw-locations-by-parent-location-id-1",
173
    "public": false,
174
    "Query": {
175
      "Criteria": {},
176
      "FacetBuilders": {},
177
      "SortClauses": {},
178
      "Filter": {
179
        "AND": [
180
          {
181
            "OR": [
182
              {
183
                "ContentTypeIdentifierCriterion": "folder"
184
              },
185
              {
186
                "ContentTypeIdentifierCriterion": "article"
187
              }
188
            ]
189
          },
190
          {
191
            "OR": [
192
              {
193
                "ContentRemoteIdCriterion": "{$foo->remoteId}"
194
              },
195
              {
196
                "ContentRemoteIdCriterion": "{$bar->remoteId}"
197
              }
198
            ]
199
          }
200
        ]
201
      },
202
      "limit": 50,
203
      "offset": 0
204
    }
205
  }
206
}
207
JSON
208
,
209
                'json',
210
                2,
211
                [$foo, $bar],
212
            ],
213
        ];
214
    }
215
216
    /**
217
     * @param \stdClass[] $contentDataList
218
     */
219
    private function createTestContentItems(array $contentDataList)
220
    {
221
        foreach ($contentDataList as $contentData) {
222
            // skip creating already created items
223
            if (in_array($contentData->remoteId, self::$createdContentRemoteIds)) {
224
                continue;
225
            }
226
227
            $this->createFolder(
228
                $contentData->name,
229
                '/api/ezp/v2/content/locations/1/2',
230
                $contentData->remoteId
231
            );
232
            self::$createdContentRemoteIds[] = $contentData->remoteId;
233
        }
234
    }
235
}
236