|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of Sulu. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) MASSIVE ART WebServices GmbH |
|
7
|
|
|
* |
|
8
|
|
|
* This source file is subject to the MIT license that is bundled |
|
9
|
|
|
* with this source code in the file LICENSE. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Functional\Controller; |
|
13
|
|
|
|
|
14
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
|
15
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
|
16
|
|
|
use Sulu\Bundle\CommentBundle\Entity\Comment; |
|
17
|
|
|
use Sulu\Bundle\CommentBundle\Entity\CommentInterface; |
|
18
|
|
|
use Sulu\Bundle\CommentBundle\Entity\Thread; |
|
19
|
|
|
use Sulu\Bundle\CommentBundle\Entity\ThreadInterface; |
|
20
|
|
|
use Sulu\Bundle\TestBundle\Testing\SuluTestCase; |
|
21
|
|
|
|
|
22
|
|
|
class CommentControllerTest extends SuluTestCase |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* @var EntityManagerInterface |
|
26
|
|
|
*/ |
|
27
|
|
|
private $entityManager; |
|
28
|
|
|
|
|
29
|
|
|
protected function setUp() |
|
30
|
|
|
{ |
|
31
|
|
|
$this->entityManager = $this->getContainer()->get('doctrine.orm.entity_manager'); |
|
32
|
|
|
|
|
33
|
|
|
$this->purgeDatabase(); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
public function testGet() |
|
37
|
|
|
{ |
|
38
|
|
|
$thread = $this->createThread(); |
|
39
|
|
|
$comment = $this->createComment($thread); |
|
40
|
|
|
$this->entityManager->flush(); |
|
41
|
|
|
$this->entityManager->clear(); |
|
42
|
|
|
|
|
43
|
|
|
$client = $this->createAuthenticatedClient(); |
|
44
|
|
|
$client->request('GET', '/api/comments/' . $comment->getId()); |
|
45
|
|
|
|
|
46
|
|
|
$this->assertHttpStatusCode(200, $client->getResponse()); |
|
47
|
|
|
$data = json_decode($client->getResponse()->getContent(), true); |
|
48
|
|
|
|
|
49
|
|
|
$this->assertEquals($comment->getId(), $data['id']); |
|
50
|
|
|
$this->assertEquals($comment->getMessage(), $data['message']); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
public function testCGet() |
|
54
|
|
|
{ |
|
55
|
|
|
$thread = $this->createThread(); |
|
56
|
|
|
$this->createComment($thread); |
|
57
|
|
|
$this->createComment($thread); |
|
58
|
|
|
$this->createComment($thread); |
|
59
|
|
|
$this->entityManager->flush(); |
|
60
|
|
|
$this->entityManager->clear(); |
|
61
|
|
|
|
|
62
|
|
|
$client = $this->createAuthenticatedClient(); |
|
63
|
|
|
$client->request('GET', '/api/comments'); |
|
64
|
|
|
|
|
65
|
|
|
$this->assertHttpStatusCode(200, $client->getResponse()); |
|
66
|
|
|
$data = json_decode($client->getResponse()->getContent(), true); |
|
67
|
|
|
|
|
68
|
|
|
$this->assertCount(3, $data['_embedded']['comments']); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
public function testCGetFilter() |
|
72
|
|
|
{ |
|
73
|
|
|
$thread = $this->createThread(); |
|
74
|
|
|
$this->createComment($thread); |
|
75
|
|
|
$this->createComment($thread, 'Message', CommentInterface::STATE_UNPUBLISHED); |
|
76
|
|
|
$this->createComment($thread); |
|
77
|
|
|
|
|
78
|
|
|
$this->entityManager->flush(); |
|
79
|
|
|
$this->entityManager->clear(); |
|
80
|
|
|
|
|
81
|
|
|
$client = $this->createAuthenticatedClient(); |
|
82
|
|
|
$client->request('GET', '/api/comments?state=' . CommentInterface::STATE_UNPUBLISHED); |
|
83
|
|
|
|
|
84
|
|
|
$this->assertHttpStatusCode(200, $client->getResponse()); |
|
85
|
|
|
$data = json_decode($client->getResponse()->getContent(), true); |
|
86
|
|
|
|
|
87
|
|
|
$this->assertCount(1, $data['_embedded']['comments']); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
public function testCGetTypeFilter() |
|
91
|
|
|
{ |
|
92
|
|
|
$thread1 = $this->createThread('Test 1', 'test-1'); |
|
93
|
|
|
$thread2 = $this->createThread('Test 2', 'test-2'); |
|
94
|
|
|
$thread3 = $this->createThread('Test 3', 'test-3'); |
|
95
|
|
|
$this->createComment($thread1); |
|
96
|
|
|
$this->createComment($thread2); |
|
97
|
|
|
$this->createComment($thread3); |
|
98
|
|
|
|
|
99
|
|
|
$this->entityManager->flush(); |
|
100
|
|
|
$this->entityManager->clear(); |
|
101
|
|
|
|
|
102
|
|
|
$client = $this->createAuthenticatedClient(); |
|
103
|
|
|
$client->request('GET', '/api/comments?threadType=test-1,test-2'); |
|
104
|
|
|
|
|
105
|
|
|
$this->assertHttpStatusCode(200, $client->getResponse()); |
|
106
|
|
|
$data = json_decode($client->getResponse()->getContent(), true); |
|
107
|
|
|
|
|
108
|
|
|
$this->assertCount(2, $data['_embedded']['comments']); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
public function testPut() |
|
112
|
|
|
{ |
|
113
|
|
|
$thread = $this->createThread(); |
|
114
|
|
|
$comment = $this->createComment($thread); |
|
115
|
|
|
$this->entityManager->flush(); |
|
116
|
|
|
$this->entityManager->clear(); |
|
117
|
|
|
|
|
118
|
|
|
$client = $this->createAuthenticatedClient(); |
|
119
|
|
|
$client->request('PUT', '/api/comments/' . $comment->getId(), ['message' => 'My new Message']); |
|
120
|
|
|
|
|
121
|
|
|
$this->assertHttpStatusCode(200, $client->getResponse()); |
|
122
|
|
|
$data = json_decode($client->getResponse()->getContent(), true); |
|
123
|
|
|
|
|
124
|
|
|
$this->assertEquals($comment->getId(), $data['id']); |
|
125
|
|
|
$this->assertEquals('My new Message', $data['message']); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
public function testDelete() |
|
129
|
|
|
{ |
|
130
|
|
|
$thread = $this->createThread(); |
|
131
|
|
|
$comment = $this->createComment($thread); |
|
132
|
|
|
$this->entityManager->flush(); |
|
133
|
|
|
$this->entityManager->clear(); |
|
134
|
|
|
|
|
135
|
|
|
$client = $this->createAuthenticatedClient(); |
|
136
|
|
|
$client->request('DELETE', '/api/comments/' . $comment->getId()); |
|
137
|
|
|
|
|
138
|
|
|
$this->assertHttpStatusCode(204, $client->getResponse()); |
|
139
|
|
|
|
|
140
|
|
|
$this->assertNull($this->entityManager->find(CommentInterface::class, $comment->getId())); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
public function testCDelete() |
|
144
|
|
|
{ |
|
145
|
|
|
$thread = $this->createThread(); |
|
146
|
|
|
$comments = [ |
|
147
|
|
|
$this->createComment($thread), |
|
148
|
|
|
$this->createComment($thread), |
|
149
|
|
|
$this->createComment($thread), |
|
150
|
|
|
]; |
|
151
|
|
|
$this->entityManager->flush(); |
|
152
|
|
|
$this->entityManager->clear(); |
|
153
|
|
|
|
|
154
|
|
|
$client = $this->createAuthenticatedClient(); |
|
155
|
|
|
$client->request( |
|
156
|
|
|
'DELETE', |
|
157
|
|
|
'/api/comments?ids=' . implode( |
|
158
|
|
|
',', |
|
159
|
|
|
array_map( |
|
160
|
|
|
function (CommentInterface $comment) { |
|
161
|
|
|
return $comment->getId(); |
|
162
|
|
|
}, |
|
163
|
|
|
[$comments[0], $comments[1]] |
|
164
|
|
|
) |
|
165
|
|
|
) |
|
166
|
|
|
); |
|
167
|
|
|
|
|
168
|
|
|
$this->assertHttpStatusCode(204, $client->getResponse()); |
|
169
|
|
|
|
|
170
|
|
|
foreach ([$comments[0], $comments[1]] as $comment) { |
|
171
|
|
|
$this->assertNull($this->entityManager->find(CommentInterface::class, $comment->getId())); |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
$this->assertNotNull($this->entityManager->find(CommentInterface::class, $comments[2]->getId())); |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
public function testPublish() |
|
178
|
|
|
{ |
|
179
|
|
|
$thread = $this->createThread(); |
|
180
|
|
|
$comment = $this->createComment($thread, 'Sulu is awesome', CommentInterface::STATE_UNPUBLISHED); |
|
181
|
|
|
$this->entityManager->flush(); |
|
182
|
|
|
$this->entityManager->clear(); |
|
183
|
|
|
|
|
184
|
|
|
$this->assertFalse($comment->isPublished()); |
|
185
|
|
|
|
|
186
|
|
|
$client = $this->createAuthenticatedClient(); |
|
187
|
|
|
$client->request('POST', '/api/comments/' . $comment->getId() . '?action=publish'); |
|
188
|
|
|
$this->assertHttpStatusCode(200, $client->getResponse()); |
|
189
|
|
|
|
|
190
|
|
|
$result = $this->entityManager->find(CommentInterface::class, $comment->getId()); |
|
191
|
|
|
$this->assertTrue($result->isPublished()); |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
public function testUnpublish() |
|
195
|
|
|
{ |
|
196
|
|
|
$thread = $this->createThread(); |
|
197
|
|
|
$comment = $this->createComment($thread); |
|
198
|
|
|
$this->entityManager->flush(); |
|
199
|
|
|
$this->entityManager->clear(); |
|
200
|
|
|
|
|
201
|
|
|
$this->assertTrue($comment->isPublished()); |
|
202
|
|
|
|
|
203
|
|
|
$client = $this->createAuthenticatedClient(); |
|
204
|
|
|
$client->request('POST', '/api/comments/' . $comment->getId() . '?action=unpublish'); |
|
205
|
|
|
$this->assertHttpStatusCode(200, $client->getResponse()); |
|
206
|
|
|
|
|
207
|
|
|
$result = $this->entityManager->find(CommentInterface::class, $comment->getId()); |
|
208
|
|
|
$this->assertFalse($result->isPublished()); |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
|
|
private function createComment( |
|
212
|
|
|
ThreadInterface $thread, |
|
213
|
|
|
$message = 'Sulu is awesome', |
|
214
|
|
|
$state = CommentInterface::STATE_PUBLISHED |
|
215
|
|
|
) { |
|
216
|
|
|
$comment = new Comment($state, $thread); |
|
217
|
|
|
$comment->setMessage($message); |
|
218
|
|
|
$thread->addComment($comment); |
|
219
|
|
|
$this->entityManager->persist($comment); |
|
220
|
|
|
|
|
221
|
|
|
return $comment; |
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
|
|
private function createThread($title = 'Sulu is awesome', $type = 'Test', $entityId = '123-123-123') |
|
225
|
|
|
{ |
|
226
|
|
|
$thread = new Thread($type, $entityId, new ArrayCollection()); |
|
227
|
|
|
$thread->setTitle($title); |
|
228
|
|
|
$this->entityManager->persist($thread); |
|
229
|
|
|
|
|
230
|
|
|
return $thread; |
|
231
|
|
|
} |
|
232
|
|
|
} |
|
233
|
|
|
|