Skip to content

Commit 1c3da89

Browse files
committed
copy&paste tests from Storyboard and StoryboardSection
1 parent 0c14982 commit 1c3da89

11 files changed

+670
-0
lines changed

api/fixtures/multiSelects.yml

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
App\Entity\ContentNode\MultiSelect:
2+
multiSelect1:
3+
root: '@columnLayout1'
4+
parent: '@columnLayout1'
5+
slot: '1'
6+
position: 0
7+
instanceName: <word()>
8+
contentType: '@contentTypeMultiSelect'
9+
multiSelect2:
10+
root: '@columnLayout1'
11+
parent: '@columnLayoutChild1'
12+
slot: '1'
13+
position: 0
14+
instanceName: <word()>
15+
contentType: '@contentTypeMultiSelect'
16+
multiSelectCampUnrelated:
17+
root: '@columnLayout1campUnrelated'
18+
parent: '@columnLayout1campUnrelated'
19+
slot: '1'
20+
position: 1
21+
instanceName: <word()>
22+
contentType: '@contentTypeMultiSelect'
23+
24+
App\Entity\ContentNode\MultiSelectOption:
25+
multiSelectOption1:
26+
multiSelect: '@multiSelect1'
27+
translateKey: <word()>
28+
checked: true
29+
multiSelectOption2:
30+
multiSelect: '@multiSelect2'
31+
translateKey: <word()>
32+
checked: true
33+
multiSelectOptionCampUnrelated:
34+
multiSelect: '@multiSelectCampUnrelated'
35+
translateKey: <word()>
36+
checked: true
37+
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
namespace App\Tests\Api\ContentNodes\MultiSelect;
4+
5+
use App\Entity\ContentNode\MultiSelect;
6+
use App\Tests\Api\ContentNodes\CreateContentNodeTestCase;
7+
8+
/**
9+
* @internal
10+
*/
11+
class CreateMultiSelectTest extends CreateContentNodeTestCase {
12+
public function setUp(): void {
13+
parent::setUp();
14+
15+
$this->endpoint = '/content_node/multi_selects';
16+
$this->entityClass = MultiSelect::class;
17+
$this->defaultContentType = static::$fixtures['contentTypeMultiSelect'];
18+
}
19+
20+
public function testCreateMultiSelectFromPrototype() {
21+
// given
22+
$prototype = static::$fixtures['multiSelect1'];
23+
$prototypeOption = static::$fixtures['multiSelectOption1'];
24+
25+
// when
26+
$response = $this->create($this->getExampleWritePayload(['prototype' => $this->getIriFor('multiSelect1')]));
27+
28+
// then
29+
$this->assertResponseStatusCodeSame(201);
30+
$this->assertCount(1, $response->toArray()['_links']['options']);
31+
$this->assertJsonContains([
32+
'_embedded' => [
33+
'options' => [
34+
[
35+
'translateKey' => $prototypeOption->translateKey,
36+
'checked' => $prototypeOption->checked,
37+
],
38+
],
39+
],
40+
]);
41+
}
42+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace App\Tests\Api\ContentNodes\MultiSelect;
4+
5+
use App\Tests\Api\ContentNodes\DeleteContentNodeTestCase;
6+
7+
/**
8+
* @internal
9+
*/
10+
class DeleteMultiSelectTest extends DeleteContentNodeTestCase {
11+
public function setUp(): void {
12+
parent::setUp();
13+
14+
$this->endpoint = '/content_node/multi_selects';
15+
$this->defaultEntity = static::$fixtures['multiSelect1'];
16+
}
17+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace App\Tests\Api\ContentNodes\MultiSelect;
4+
5+
use App\Tests\Api\ContentNodes\ListContentNodeTestCase;
6+
7+
/**
8+
* @internal
9+
*/
10+
class ListMultiSelectTest extends ListContentNodeTestCase {
11+
public function setUp(): void {
12+
parent::setUp();
13+
14+
$this->endpoint = '/content_node/multi_selects';
15+
16+
$this->contentNodesCamp1and2 = [
17+
$this->getIriFor('multiSelect1'),
18+
$this->getIriFor('multiSelect2'),
19+
];
20+
21+
$this->contentNodesCampUnrelated = [
22+
$this->getIriFor('multiSelectCampUnrelated'),
23+
];
24+
}
25+
26+
public function testListMultiSelectsFilteredByParent() {
27+
$response = static::createClientWithCredentials()->request('GET', "{$this->endpoint}?parent=".$this->getIriFor('columnLayout1'));
28+
$this->assertResponseStatusCodeSame(200);
29+
30+
$this->assertJsonContainsItems($response, [$this->getIriFor('multiSelect1')]);
31+
}
32+
}
Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
<?php
2+
3+
namespace App\Tests\Api\ContentNodes\MultiSelect\Option;
4+
5+
use App\Entity\ContentNode\MultiSelect;
6+
use App\Entity\ContentNode\MultiSelectOption;
7+
use App\Tests\Api\ECampApiTestCase;
8+
9+
/**
10+
* @internal
11+
*/
12+
class CreateMultiSelectOptionTest extends ECampApiTestCase {
13+
protected MultiSelect $defaultStorybord;
14+
15+
public function setUp(): void {
16+
parent::setUp();
17+
18+
$this->endpoint = '/content_node/multi_select_options';
19+
$this->entityClass = MultiSelectOption::class;
20+
21+
$this->defaultStorybord = static::$fixtures['multiSelect1'];
22+
}
23+
24+
public function testCreateCleansHTMLFromText() {
25+
// given
26+
$text = ' testText<script>alert(1)</script>';
27+
28+
// when
29+
$this->create($this->getExampleWritePayload([
30+
'translateKey' => $text,
31+
'checked' => true,
32+
]));
33+
34+
// then
35+
$textSanitized = ' testText';
36+
$this->assertResponseStatusCodeSame(201);
37+
$this->assertJsonContains([
38+
'translateKey' => $textSanitized,
39+
'checked' => true,
40+
]);
41+
}
42+
43+
/**
44+
* Standard security checks.
45+
*/
46+
public function testCreateIsDeniedForAnonymousUser() {
47+
static::createBasicClient()->request('POST', $this->endpoint, ['json' => $this->getExampleWritePayload()]);
48+
$this->assertResponseStatusCodeSame(401);
49+
$this->assertJsonContains([
50+
'code' => 401,
51+
'message' => 'JWT Token not found',
52+
]);
53+
}
54+
55+
public function testCreateIsDeniedForInvitedCollaborator() {
56+
// when
57+
$this->create(user: static::$fixtures['user6invited']);
58+
59+
// then: throws "item not found" because multiSelect1 is not readable
60+
$this->assertResponseStatusCodeSame(400);
61+
$this->assertJsonContains([
62+
'title' => 'An error occurred',
63+
'detail' => 'Item not found for "'.$this->getIriFor($this->defaultStorybord).'".',
64+
]);
65+
}
66+
67+
public function testCreateIsDeniedForInactiveCollaborator() {
68+
// when
69+
$this->create(user: static::$fixtures['user5inactive']);
70+
71+
// then: throws "item not found" because multiSelect1 is not readable
72+
$this->assertResponseStatusCodeSame(400);
73+
$this->assertJsonContains([
74+
'title' => 'An error occurred',
75+
'detail' => 'Item not found for "'.$this->getIriFor($this->defaultStorybord).'".',
76+
]);
77+
}
78+
79+
public function testCreateIsDeniedForUnrelatedUser() {
80+
// when
81+
$this->create(user: static::$fixtures['user4unrelated']);
82+
83+
// then: throws "item not found" because multiSelect1 is not readable
84+
$this->assertResponseStatusCodeSame(400);
85+
$this->assertJsonContains([
86+
'title' => 'An error occurred',
87+
'detail' => 'Item not found for "'.$this->getIriFor($this->defaultStorybord).'".',
88+
]);
89+
}
90+
91+
public function testCreateIsDeniedForGuest() {
92+
// when
93+
$this->create(user: static::$fixtures['user3guest']);
94+
95+
// then
96+
$this->assertResponseStatusCodeSame(403);
97+
$this->assertJsonContains([
98+
'title' => 'An error occurred',
99+
'detail' => 'Access Denied.',
100+
]);
101+
}
102+
103+
public function testCreateIsAllowedForMember() {
104+
$this->create(user: static::$fixtures['user2member']);
105+
$this->assertResponseStatusCodeSame(201);
106+
}
107+
108+
public function testCreateIsAllowedForManager() {
109+
// when
110+
$response = $this->create(user: static::$fixtures['user1manager']);
111+
$id = $response->toArray()['id'];
112+
$newMultiSelectOption = $this->getEntityManager()->getRepository($this->entityClass)->find($id);
113+
114+
// then
115+
$this->assertResponseStatusCodeSame(201);
116+
$this->assertJsonContains($this->getExampleReadPayload($newMultiSelectOption), true);
117+
}
118+
119+
/**
120+
* Payload setup.
121+
*/
122+
protected function getExampleWritePayload($attributes = [], $except = []) {
123+
return parent::getExampleWritePayload(
124+
array_merge([
125+
'multiSelect' => $this->getIriFor($this->defaultStorybord),
126+
'translateKey' => 'key',
127+
'checked' => true,
128+
], $attributes),
129+
$except
130+
);
131+
}
132+
133+
protected function getExampleReadPayload(MultiSelectOption $self, $attributes = [], $except = []) {
134+
/** @var MultiSelect $multiSelect */
135+
$multiSelect = $this->defaultStorybord;
136+
137+
return [
138+
'translateKey' => 'key',
139+
'checked' => true,
140+
'_links' => [
141+
'self' => [
142+
'href' => $this->getIriFor($self),
143+
],
144+
'multiSelect' => [
145+
'href' => $this->getIriFor($multiSelect),
146+
],
147+
],
148+
];
149+
}
150+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
3+
namespace App\Tests\Api\ContentNodes\MultiSelect\Option;
4+
5+
use App\Tests\Api\ECampApiTestCase;
6+
7+
/**
8+
* @internal
9+
*/
10+
class DeleteMultiSelectOptionTest extends ECampApiTestCase {
11+
public function setUp(): void {
12+
parent::setUp();
13+
14+
$this->endpoint = '/content_node/multi_select_options';
15+
$this->defaultEntity = static::$fixtures['multiSelectOption1'];
16+
}
17+
18+
public function testDeleteIsDeniedForAnonymousUser() {
19+
static::createBasicClient()->request('DELETE', "{$this->endpoint}/".$this->defaultEntity->getId());
20+
$this->assertResponseStatusCodeSame(401);
21+
$this->assertJsonContains([
22+
'code' => 401,
23+
'message' => 'JWT Token not found',
24+
]);
25+
26+
$this->assertEntityStillExists();
27+
}
28+
29+
public function testDeleteIsDeniedForInvitedCollaborator() {
30+
$this->delete(user: static::$fixtures['user6invited']);
31+
$this->assertResponseStatusCodeSame(404);
32+
$this->assertEntityStillExists();
33+
}
34+
35+
public function testDeleteIsDeniedForInactiveCollaborator() {
36+
$this->delete(user: static::$fixtures['user5inactive']);
37+
$this->assertResponseStatusCodeSame(404);
38+
$this->assertEntityStillExists();
39+
}
40+
41+
public function testDeleteIsDeniedForUnrelatedUser() {
42+
$this->delete(user: static::$fixtures['user4unrelated']);
43+
$this->assertResponseStatusCodeSame(404);
44+
$this->assertEntityStillExists();
45+
}
46+
47+
public function testDeleteIsDeniedForGuest() {
48+
$this->delete(user: static::$fixtures['user3guest']);
49+
$this->assertResponseStatusCodeSame(403);
50+
$this->assertEntityStillExists();
51+
}
52+
53+
public function testDeleteIsAllowedForMember() {
54+
$this->delete(user: static::$fixtures['user2member']);
55+
$this->assertResponseStatusCodeSame(204);
56+
$this->assertEntityWasRemoved();
57+
}
58+
59+
public function testDeleteIsAllowedForManager() {
60+
$this->delete(user: static::$fixtures['user1manager']);
61+
$this->assertResponseStatusCodeSame(204);
62+
$this->assertEntityWasRemoved();
63+
}
64+
}

0 commit comments

Comments
 (0)