3 namespace Tests\AnnotationBundle\Controller
;
5 use Tests\Wallabag\AnnotationBundle\WallabagAnnotationTestCase
;
6 use Wallabag\AnnotationBundle\Entity\Annotation
;
7 use Wallabag\CoreBundle\Entity\Entry
;
9 class AnnotationControllerTest
extends WallabagAnnotationTestCase
12 * Test fetching annotations for an entry.
14 public function testGetAnnotations()
16 /** @var Annotation $annotation */
17 $annotation = $this->client
->getContainer()
18 ->get('doctrine.orm.entity_manager')
19 ->getRepository('WallabagAnnotationBundle:Annotation')
20 ->findOneByUsername('admin');
23 $this->markTestSkipped('No content found in db.');
26 $this->logInAs('admin');
27 $this->client
->request('GET', 'annotations/'.$annotation->getEntry()->getId().'.json');
28 $this->assertEquals(200, $this->client
->getResponse()->getStatusCode());
30 $content = json_decode($this->client
->getResponse()->getContent(), true);
31 $this->assertEquals(1, $content['total']);
32 $this->assertEquals($annotation->getText(), $content['rows'][0]['text']);
36 * Test creating an annotation for an entry.
38 public function testSetAnnotation()
40 $this->logInAs('admin');
42 /** @var Entry $entry */
43 $entry = $this->client
->getContainer()
44 ->get('doctrine.orm.entity_manager')
45 ->getRepository('WallabagCoreBundle:Entry')
46 ->findOneByUsernameAndNotArchived('admin');
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],
54 $this->client
->request('POST', 'annotations/'.$entry->getId().'.json', [], [], $headers, $content);
56 $this->assertEquals(200, $this->client
->getResponse()->getStatusCode());
58 $content = json_decode($this->client
->getResponse()->getContent(), true);
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']);
65 /** @var Annotation $annotation */
66 $annotation = $this->client
->getContainer()
67 ->get('doctrine.orm.entity_manager')
68 ->getRepository('WallabagAnnotationBundle:Annotation')
69 ->findLastAnnotationByPageId($entry->getId(), 1);
71 $this->assertEquals('my annotation', $annotation->getText());
75 * Test editing an existing annotation.
77 public function testEditAnnotation()
79 /** @var Annotation $annotation */
80 $annotation = $this->client
->getContainer()
81 ->get('doctrine.orm.entity_manager')
82 ->getRepository('WallabagAnnotationBundle:Annotation')
83 ->findOneByUsername('admin');
85 $this->logInAs('admin');
87 $headers = ['CONTENT_TYPE' => 'application/json'];
88 $content = json_encode([
89 'text' => 'a modified annotation',
91 $this->client
->request('PUT', 'annotations/'.$annotation->getId().'.json', [], [], $headers, $content);
92 $this->assertEquals(200, $this->client
->getResponse()->getStatusCode());
94 $content = json_decode($this->client
->getResponse()->getContent(), true);
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']);
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());
110 * Test deleting an annotation.
112 public function testDeleteAnnotation()
114 /** @var Annotation $annotation */
115 $annotation = $this->client
->getContainer()
116 ->get('doctrine.orm.entity_manager')
117 ->getRepository('WallabagAnnotationBundle:Annotation')
118 ->findOneByUsername('admin');
120 $this->logInAs('admin');
122 $headers = ['CONTENT_TYPE' => 'application/json'];
123 $content = json_encode([
124 'text' => 'a modified annotation',
126 $this->client
->request('DELETE', 'annotations/'.$annotation->getId().'.json', [], [], $headers, $content);
127 $this->assertEquals(200, $this->client
->getResponse()->getStatusCode());
129 $content = json_decode($this->client
->getResponse()->getContent(), true);
131 $this->assertEquals('a modified annotation', $content['text']);
133 $annotationDeleted = $this->client
->getContainer()
134 ->get('doctrine.orm.entity_manager')
135 ->getRepository('WallabagAnnotationBundle:Annotation')
136 ->findOneById($annotation->getId());
138 $this->assertNull($annotationDeleted);