]> git.immae.eu Git - github/wallabag/wallabag.git/blame - tests/Wallabag/AnnotationBundle/Controller/AnnotationControllerTest.php
fix cs and phpdoc
[github/wallabag/wallabag.git] / tests / Wallabag / AnnotationBundle / Controller / AnnotationControllerTest.php
CommitLineData
f38e03dc
TC
1<?php
2
23634d5d 3namespace Tests\AnnotationBundle\Controller;
f38e03dc 4
23634d5d 5use Tests\Wallabag\AnnotationBundle\WallabagAnnotationTestCase;
0c271b9e
TC
6use Wallabag\AnnotationBundle\Entity\Annotation;
7use Wallabag\CoreBundle\Entity\Entry;
f38e03dc 8
4dc87223 9class AnnotationControllerTest extends WallabagAnnotationTestCase
f38e03dc 10{
0c271b9e
TC
11 /**
12 * Test fetching annotations for an entry
13 */
4dc87223 14 public function testGetAnnotations()
f38e03dc 15 {
0c271b9e 16 /** @var Annotation $annotation */
4dc87223 17 $annotation = $this->client->getContainer()
f38e03dc 18 ->get('doctrine.orm.entity_manager')
4dc87223 19 ->getRepository('WallabagAnnotationBundle:Annotation')
09d8bb6f 20 ->findOneByUsername('admin');
f38e03dc 21
4dc87223 22 if (!$annotation) {
f38e03dc
TC
23 $this->markTestSkipped('No content found in db.');
24 }
09d8bb6f 25
f38e03dc 26 $this->logInAs('admin');
0c271b9e 27 $this->client->request('GET', 'annotations/'.$annotation->getEntry()->getId().'.json');
f38e03dc
TC
28 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
29
30 $content = json_decode($this->client->getResponse()->getContent(), true);
31 $this->assertEquals(1, $content['total']);
4dc87223 32 $this->assertEquals($annotation->getText(), $content['rows'][0]['text']);
f38e03dc
TC
33 }
34
0c271b9e
TC
35 /**
36 * Test creating an annotation for an entry
37 */
4dc87223 38 public function testSetAnnotation()
f38e03dc
TC
39 {
40 $this->logInAs('admin');
41
0c271b9e 42 /** @var Entry $entry */
f38e03dc
TC
43 $entry = $this->client->getContainer()
44 ->get('doctrine.orm.entity_manager')
45 ->getRepository('WallabagCoreBundle:Entry')
09d8bb6f 46 ->findOneByUsernameAndNotArchived('admin');
f38e03dc 47
4094ea47
JB
48 $headers = ['CONTENT_TYPE' => 'application/json'];
49 $content = json_encode([
4dc87223 50 'text' => 'my annotation',
f38e03dc 51 'quote' => 'my quote',
4094ea47
JB
52 'ranges' => ['start' => '', 'startOffset' => 24, 'end' => '', 'endOffset' => 31],
53 ]);
0c271b9e 54 $this->client->request('POST', 'annotations/'.$entry->getId().'.json', [], [], $headers, $content);
f38e03dc
TC
55
56 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
57
09d8bb6f
JB
58 $content = json_decode($this->client->getResponse()->getContent(), true);
59
60 $this->assertEquals('Big boss', $content['user']);
61 $this->assertEquals('v1.0', $content['annotator_schema_version']);
62 $this->assertEquals('my annotation', $content['text']);
63 $this->assertEquals('my quote', $content['quote']);
64
0c271b9e 65 /** @var Annotation $annotation */
4dc87223 66 $annotation = $this->client->getContainer()
f38e03dc 67 ->get('doctrine.orm.entity_manager')
4dc87223
NL
68 ->getRepository('WallabagAnnotationBundle:Annotation')
69 ->findLastAnnotationByPageId($entry->getId(), 1);
f38e03dc 70
4dc87223 71 $this->assertEquals('my annotation', $annotation->getText());
f38e03dc
TC
72 }
73
0c271b9e
TC
74 /**
75 * Test editing an existing annotation
76 */
4dc87223 77 public function testEditAnnotation()
f38e03dc 78 {
0c271b9e 79 /** @var Annotation $annotation */
4dc87223 80 $annotation = $this->client->getContainer()
f38e03dc 81 ->get('doctrine.orm.entity_manager')
4dc87223 82 ->getRepository('WallabagAnnotationBundle:Annotation')
09d8bb6f 83 ->findOneByUsername('admin');
f38e03dc
TC
84
85 $this->logInAs('admin');
86
4094ea47
JB
87 $headers = ['CONTENT_TYPE' => 'application/json'];
88 $content = json_encode([
4dc87223 89 'text' => 'a modified annotation',
4094ea47 90 ]);
0c271b9e 91 $this->client->request('PUT', 'annotations/'.$annotation->getId().'.json', [], [], $headers, $content);
f38e03dc
TC
92 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
93
94 $content = json_decode($this->client->getResponse()->getContent(), true);
95
09d8bb6f
JB
96 $this->assertEquals('Big boss', $content['user']);
97 $this->assertEquals('v1.0', $content['annotator_schema_version']);
4dc87223 98 $this->assertEquals('a modified annotation', $content['text']);
b95ffda2 99 $this->assertEquals('my quote', $content['quote']);
f38e03dc 100
0c271b9e 101 /** @var Annotation $annotationUpdated */
4dc87223 102 $annotationUpdated = $this->client->getContainer()
f38e03dc 103 ->get('doctrine.orm.entity_manager')
4dc87223 104 ->getRepository('WallabagAnnotationBundle:Annotation')
09d8bb6f 105 ->findOneById($annotation->getId());
4dc87223 106 $this->assertEquals('a modified annotation', $annotationUpdated->getText());
f38e03dc 107 }
09d8bb6f 108
0c271b9e
TC
109 /**
110 * Test deleting an annotation
111 */
09d8bb6f
JB
112 public function testDeleteAnnotation()
113 {
0c271b9e 114 /** @var Annotation $annotation */
09d8bb6f
JB
115 $annotation = $this->client->getContainer()
116 ->get('doctrine.orm.entity_manager')
117 ->getRepository('WallabagAnnotationBundle:Annotation')
118 ->findOneByUsername('admin');
119
120 $this->logInAs('admin');
121
4094ea47
JB
122 $headers = ['CONTENT_TYPE' => 'application/json'];
123 $content = json_encode([
09d8bb6f 124 'text' => 'a modified annotation',
4094ea47 125 ]);
0c271b9e 126 $this->client->request('DELETE', 'annotations/'.$annotation->getId().'.json', [], [], $headers, $content);
09d8bb6f
JB
127 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
128
129 $content = json_decode($this->client->getResponse()->getContent(), true);
130
131 $this->assertEquals('a modified annotation', $content['text']);
132
133 $annotationDeleted = $this->client->getContainer()
134 ->get('doctrine.orm.entity_manager')
135 ->getRepository('WallabagAnnotationBundle:Annotation')
136 ->findOneById($annotation->getId());
137
138 $this->assertNull($annotationDeleted);
139 }
f38e03dc 140}