Skip to content

Commit 7d1a9cd

Browse files
authored
Merge pull request #8131 from simfeld/comments-api-improvements
Add createTime to comments and add link to activity
2 parents d0ea399 + 2979ce4 commit 7d1a9cd

13 files changed

+178
-0
lines changed

api/src/Entity/Activity.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,7 @@ class Activity extends BaseEntity implements BelongsToCampInterface {
185185
uriTemplate: Comment::ACTIVITY_SUBRESOURCE_URI_TEMPLATE,
186186
example: '/activities/1a2b3c4d/comments'
187187
)]
188+
#[Groups(['read'])]
188189
#[ORM\OneToMany(targetEntity: Comment::class, mappedBy: 'activity')]
189190
public Collection $comments;
190191

api/src/Entity/Comment.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
],
5454
denormalizationContext: ['groups' => ['write']],
5555
normalizationContext: ['groups' => ['read']],
56+
order: ['createTime' => 'ASC'],
5657
)]
5758
#[ApiFilter(filterClass: SearchFilter::class, properties: ['camp', 'activity'])]
5859
#[ORM\Entity(repositoryClass: CommentRepository::class)]
@@ -120,4 +121,10 @@ public function __construct() {
120121
public function getCamp(): ?Camp {
121122
return $this->camp;
122123
}
124+
125+
#[ApiProperty(writable: false)]
126+
#[Groups(['read'])]
127+
public function getCreateTime(): \DateTime {
128+
return $this->createTime;
129+
}
123130
}

api/tests/Api/Activities/ReadActivityTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ public function testGetSingleActivityIsAllowedForGuest() {
6464
'camp' => ['href' => $this->getIriFor('camp1')],
6565
'scheduleEntries' => ['href' => '/schedule_entries?activity=%2Factivities%2F'.$activity->getId()],
6666
'activityResponsibles' => ['href' => '/activity_responsibles?activity=%2Factivities%2F'.$activity->getId()],
67+
'comments' => ['href' => '/activities/'.$activity->getId().'/comments'],
6768
],
6869
]);
6970
}
@@ -86,6 +87,7 @@ public function testGetSingleActivityIsAllowedForMember() {
8687
'camp' => ['href' => $this->getIriFor('camp1')],
8788
'scheduleEntries' => ['href' => '/schedule_entries?activity=%2Factivities%2F'.$activity->getId()],
8889
'activityResponsibles' => ['href' => '/activity_responsibles?activity=%2Factivities%2F'.$activity->getId()],
90+
'comments' => ['href' => '/activities/'.$activity->getId().'/comments'],
8991
],
9092
]);
9193

@@ -109,6 +111,7 @@ public function testGetSingleActivityIsAllowedForManager() {
109111
'camp' => ['href' => $this->getIriFor('camp1')],
110112
'scheduleEntries' => ['href' => '/schedule_entries?activity=%2Factivities%2F'.$activity->getId()],
111113
'activityResponsibles' => ['href' => '/activity_responsibles?activity=%2Factivities%2F'.$activity->getId()],
114+
'comments' => ['href' => '/activities/'.$activity->getId().'/comments'],
112115
],
113116
]);
114117
}

api/tests/Api/Comments/ListCommentsTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace App\Tests\Api\Comments;
44

5+
use ApiPlatform\Metadata\Post;
6+
use App\Entity\Comment;
57
use App\Tests\Api\ECampApiTestCase;
68

79
/**
@@ -30,6 +32,30 @@ public function testListCommentsIsAllowedForLoggedInUser() {
3032
]);
3133
}
3234

35+
public function testListCommentsSortyByCreateTime() {
36+
$client = static::createClientWithCredentials();
37+
$client->disableReboot();
38+
39+
// create another comment so that it has a different createTime than the existing fixtures
40+
$lastComment = $client->request('POST', '/comments', ['json' => $this->getExamplePayload(
41+
Comment::class,
42+
Post::class,
43+
[
44+
'camp' => $this->getIriFor('camp1'),
45+
'activity' => $this->getIriFor('activity1'),
46+
],
47+
[],
48+
[]
49+
)])->toArray();
50+
51+
$response = $client->request('GET', '/comments');
52+
$items = $response->toArray()['_embedded']['items'];
53+
54+
$this->assertCount(4, $items);
55+
$this->assertGreaterThanOrEqual($items[0]['createTime'], $items[3]['createTime']);
56+
$this->assertEquals($items[3]['createTime'], $lastComment['createTime']);
57+
}
58+
3359
public function testListCommentsFilteredByActivity() {
3460
$activity = static::getFixture('activity1');
3561
$response = static::createClientWithCredentials()->request('GET', '/comments?activity='.$this->getIriFor($activity));

api/tests/Api/Comments/ReadCommentTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ public function testGetSingleCommentIsAllowedForAuthor() {
2929
$this->assertJsonContains([
3030
'id' => $comment->getId(),
3131
'textHtml' => $comment->textHtml,
32+
'createTime' => $comment->getCreateTime()->format(\DateTime::W3C),
3233
]);
3334
}
3435

@@ -42,6 +43,7 @@ public function testGetSingleCommentIsAllowedForCollaborator() {
4243
$this->assertJsonContains([
4344
'id' => $comment->getId(),
4445
'textHtml' => $comment->textHtml,
46+
'createTime' => $comment->getCreateTime()->format(\DateTime::W3C),
4547
]);
4648
}
4749

api/tests/Api/SnapshotTests/__snapshots__/ResponseSnapshotTest__testGetCollectionMatchesStructure with data set activities__1.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@
4242
"category": {
4343
"href": "escaped_value"
4444
},
45+
"comments": {
46+
"href": "escaped_value"
47+
},
4548
"contentNodes": {
4649
"href": "escaped_value"
4750
},
@@ -140,6 +143,9 @@
140143
"category": {
141144
"href": "escaped_value"
142145
},
146+
"comments": {
147+
"href": "escaped_value"
148+
},
143149
"contentNodes": {
144150
"href": "escaped_value"
145151
},
@@ -238,6 +244,9 @@
238244
"category": {
239245
"href": "escaped_value"
240246
},
247+
"comments": {
248+
"href": "escaped_value"
249+
},
241250
"contentNodes": {
242251
"href": "escaped_value"
243252
},
@@ -324,6 +333,9 @@
324333
"category": {
325334
"href": "escaped_value"
326335
},
336+
"comments": {
337+
"href": "escaped_value"
338+
},
327339
"contentNodes": {
328340
"href": "escaped_value"
329341
},

api/tests/Api/SnapshotTests/__snapshots__/ResponseSnapshotTest__testGetCollectionMatchesStructure with data set comments__1.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
"href": "escaped_value"
1717
}
1818
},
19+
"createTime": "escaped_value",
1920
"id": "escaped_value",
2021
"orphanDescription": "escaped_value",
2122
"textHtml": "escaped_value"
@@ -35,6 +36,7 @@
3536
"href": "escaped_value"
3637
}
3738
},
39+
"createTime": "escaped_value",
3840
"id": "escaped_value",
3941
"orphanDescription": "escaped_value",
4042
"textHtml": "escaped_value"
@@ -54,6 +56,7 @@
5456
"href": "escaped_value"
5557
}
5658
},
59+
"createTime": "escaped_value",
5760
"id": "escaped_value",
5861
"orphanDescription": "escaped_value",
5962
"textHtml": "escaped_value"

api/tests/Api/SnapshotTests/__snapshots__/ResponseSnapshotTest__testGetItemMatchesStructure with data set activities__1.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -520,6 +520,9 @@
520520
"category": {
521521
"href": "escaped_value"
522522
},
523+
"comments": {
524+
"href": "escaped_value"
525+
},
523526
"contentNodes": {
524527
"href": "escaped_value"
525528
},

api/tests/Api/SnapshotTests/__snapshots__/ResponseSnapshotTest__testGetItemMatchesStructure with data set comments__1.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
"href": "escaped_value"
1414
}
1515
},
16+
"createTime": "escaped_value",
1617
"id": "escaped_value",
1718
"orphanDescription": "escaped_value",
1819
"textHtml": "escaped_value"

api/tests/Api/SnapshotTests/__snapshots__/ResponseSnapshotTest__testGetItemMatchesStructure with data set schedule_entries__1.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
"category": {
1212
"href": "escaped_value"
1313
},
14+
"comments": {
15+
"href": "escaped_value"
16+
},
1417
"contentNodes": {
1518
"href": "escaped_value"
1619
},

0 commit comments

Comments
 (0)