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