aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--src/Wallabag/AnnotationBundle/Repository/AnnotationRepository.php6
-rw-r--r--src/Wallabag/ApiBundle/Controller/WallabagRestController.php33
-rw-r--r--tests/Wallabag/AnnotationBundle/Controller/AnnotationControllerTest.php136
3 files changed, 120 insertions, 55 deletions
diff --git a/src/Wallabag/AnnotationBundle/Repository/AnnotationRepository.php b/src/Wallabag/AnnotationBundle/Repository/AnnotationRepository.php
index 5f7da70e..8cccffba 100644
--- a/src/Wallabag/AnnotationBundle/Repository/AnnotationRepository.php
+++ b/src/Wallabag/AnnotationBundle/Repository/AnnotationRepository.php
@@ -50,7 +50,8 @@ class AnnotationRepository extends EntityRepository
50 { 50 {
51 return $this->createQueryBuilder('a') 51 return $this->createQueryBuilder('a')
52 ->andWhere('a.id = :annotationId')->setParameter('annotationId', $annotationId) 52 ->andWhere('a.id = :annotationId')->setParameter('annotationId', $annotationId)
53 ->getQuery()->getSingleResult() 53 ->getQuery()
54 ->getSingleResult()
54 ; 55 ;
55 } 56 }
56 57
@@ -67,7 +68,8 @@ class AnnotationRepository extends EntityRepository
67 return $this->createQueryBuilder('a') 68 return $this->createQueryBuilder('a')
68 ->where('a.entry = :entryId')->setParameter('entryId', $entryId) 69 ->where('a.entry = :entryId')->setParameter('entryId', $entryId)
69 ->andwhere('a.user = :userId')->setParameter('userId', $userId) 70 ->andwhere('a.user = :userId')->setParameter('userId', $userId)
70 ->getQuery()->getResult() 71 ->getQuery()
72 ->getResult()
71 ; 73 ;
72 } 74 }
73 75
diff --git a/src/Wallabag/ApiBundle/Controller/WallabagRestController.php b/src/Wallabag/ApiBundle/Controller/WallabagRestController.php
index 0c709ca0..a73d44ca 100644
--- a/src/Wallabag/ApiBundle/Controller/WallabagRestController.php
+++ b/src/Wallabag/ApiBundle/Controller/WallabagRestController.php
@@ -536,7 +536,7 @@ class WallabagRestController extends FOSRestController
536 { 536 {
537 $this->validateAuthentication(); 537 $this->validateAuthentication();
538 538
539 return $this->forward('WallabagApiBundle:WallabagRest:getAnnotations', [ 539 return $this->forward('WallabagAnnotationBundle:WallabagAnnotation:getAnnotations', [
540 'entry' => $entry, 540 'entry' => $entry,
541 ]); 541 ]);
542 } 542 }
@@ -544,10 +544,6 @@ class WallabagRestController extends FOSRestController
544 /** 544 /**
545 * Creates a new annotation. 545 * Creates a new annotation.
546 * 546 *
547 * @param Request $request
548 * @param Entry $entry
549 *
550 * @return JsonResponse
551 * @ApiDoc( 547 * @ApiDoc(
552 * requirements={ 548 * requirements={
553 * {"name"="ranges", "dataType"="array", "requirement"="\w+", "description"="The range array for the annotation"}, 549 * {"name"="ranges", "dataType"="array", "requirement"="\w+", "description"="The range array for the annotation"},
@@ -555,15 +551,20 @@ class WallabagRestController extends FOSRestController
555 * {"name"="text", "dataType"="string", "required"=true, "description"=""}, 551 * {"name"="text", "dataType"="string", "required"=true, "description"=""},
556 * } 552 * }
557 * ) 553 * )
554 *
555 * @param Request $request
556 * @param Entry $entry
557 *
558 * @return JsonResponse
558 */ 559 */
559 public function postAnnotationAction(Request $request, Entry $entry) 560 public function postAnnotationAction(Request $request, Entry $entry)
560 { 561 {
561 $this->validateAuthentication(); 562 $this->validateAuthentication();
562 563
563 return $this->forward('WallabagApiBundle:WallabagRest:postAnnotation', [ 564 return $this->forward('WallabagAnnotationBundle:WallabagAnnotation:postAnnotation', [
564 'request' => $request, 565 'request' => $request,
565 'entry' => $entry, 566 'entry' => $entry,
566 ]); 567 ]);
567 } 568 }
568 569
569 /** 570 /**
@@ -586,10 +587,10 @@ class WallabagRestController extends FOSRestController
586 { 587 {
587 $this->validateAuthentication(); 588 $this->validateAuthentication();
588 589
589 return $this->forward('WallabagApiBundle:WallabagRest:putAnnotation', [ 590 return $this->forward('WallabagAnnotationBundle:WallabagAnnotation:putAnnotation', [
590 'annotation' => $annotation, 591 'annotation' => $annotation,
591 'request' => $request, 592 'request' => $request,
592 ]); 593 ]);
593 } 594 }
594 595
595 /** 596 /**
@@ -611,9 +612,9 @@ class WallabagRestController extends FOSRestController
611 { 612 {
612 $this->validateAuthentication(); 613 $this->validateAuthentication();
613 614
614 return $this->forward('WallabagApiBundle:WallabagRest:deleteAnnotation', [ 615 return $this->forward('WallabagAnnotationBundle:WallabagAnnotation:deleteAnnotation', [
615 'annotation' => $annotation, 616 'annotation' => $annotation,
616 ]); 617 ]);
617 } 618 }
618 619
619 /** 620 /**
diff --git a/tests/Wallabag/AnnotationBundle/Controller/AnnotationControllerTest.php b/tests/Wallabag/AnnotationBundle/Controller/AnnotationControllerTest.php
index 8c23de45..cee0b847 100644
--- a/tests/Wallabag/AnnotationBundle/Controller/AnnotationControllerTest.php
+++ b/tests/Wallabag/AnnotationBundle/Controller/AnnotationControllerTest.php
@@ -9,39 +9,74 @@ use Wallabag\CoreBundle\Entity\Entry;
9class AnnotationControllerTest extends WallabagAnnotationTestCase 9class AnnotationControllerTest extends WallabagAnnotationTestCase
10{ 10{
11 /** 11 /**
12 * This data provider allow to tests annotation from the :
13 * - API POV (when user use the api to manage annotations)
14 * - and User POV (when user use the web interface - using javascript - to manage annotations)
15 */
16 public function dataForEachAnnotations()
17 {
18 return [
19 ['/api/annotations'],
20 ['annotations'],
21 ];
22 }
23
24 /**
12 * Test fetching annotations for an entry. 25 * Test fetching annotations for an entry.
26 *
27 * @dataProvider dataForEachAnnotations
13 */ 28 */
14 public function testGetAnnotations() 29 public function testGetAnnotations($prefixUrl)
15 { 30 {
16 /** @var Annotation $annotation */ 31 $em = $this->client->getContainer()->get('doctrine.orm.entity_manager');
17 $annotation = $this->client->getContainer() 32
18 ->get('doctrine.orm.entity_manager') 33 $user = $em
19 ->getRepository('WallabagAnnotationBundle:Annotation') 34 ->getRepository('WallabagUserBundle:User')
20 ->findOneByUsername('admin'); 35 ->findOneByUserName('admin');
36 $entry = $em
37 ->getRepository('WallabagCoreBundle:Entry')
38 ->findOneByUsernameAndNotArchived('admin');
39
40 $annotation = new Annotation($user);
41 $annotation->setEntry($entry);
42 $annotation->setText('This is my annotation /o/');
43 $annotation->setQuote('content');
21 44
22 if (!$annotation) { 45 $em->persist($annotation);
23 $this->markTestSkipped('No content found in db.'); 46 $em->flush();
47
48 if ('annotations' === $prefixUrl) {
49 $this->logInAs('admin');
24 } 50 }
25 51
26 $this->logInAs('admin'); 52 $this->client->request('GET', $prefixUrl.'/'.$entry->getId().'.json');
27 $this->client->request('GET', 'annotations/'.$annotation->getEntry()->getId().'.json');
28 $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); 53 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
29 54
30 $content = json_decode($this->client->getResponse()->getContent(), true); 55 $content = json_decode($this->client->getResponse()->getContent(), true);
31 $this->assertEquals(1, $content['total']); 56 $this->assertGreaterThanOrEqual(1, $content['total']);
32 $this->assertEquals($annotation->getText(), $content['rows'][0]['text']); 57 $this->assertEquals($annotation->getText(), $content['rows'][0]['text']);
58
59 // we need to re-fetch the annotation becase after the flush, it has been "detached" from the entity manager
60 $annotation = $em->getRepository('WallabagAnnotationBundle:Annotation')->findAnnotationById($annotation->getId());
61 $em->remove($annotation);
62 $em->flush();
33 } 63 }
34 64
35 /** 65 /**
36 * Test creating an annotation for an entry. 66 * Test creating an annotation for an entry.
67 *
68 * @dataProvider dataForEachAnnotations
37 */ 69 */
38 public function testSetAnnotation() 70 public function testSetAnnotation($prefixUrl)
39 { 71 {
40 $this->logInAs('admin'); 72 $em = $this->client->getContainer()->get('doctrine.orm.entity_manager');
73
74 if ('annotations' === $prefixUrl) {
75 $this->logInAs('admin');
76 }
41 77
42 /** @var Entry $entry */ 78 /** @var Entry $entry */
43 $entry = $this->client->getContainer() 79 $entry = $em
44 ->get('doctrine.orm.entity_manager')
45 ->getRepository('WallabagCoreBundle:Entry') 80 ->getRepository('WallabagCoreBundle:Entry')
46 ->findOneByUsernameAndNotArchived('admin'); 81 ->findOneByUsernameAndNotArchived('admin');
47 82
@@ -51,7 +86,7 @@ class AnnotationControllerTest extends WallabagAnnotationTestCase
51 'quote' => 'my quote', 86 'quote' => 'my quote',
52 'ranges' => ['start' => '', 'startOffset' => 24, 'end' => '', 'endOffset' => 31], 87 'ranges' => ['start' => '', 'startOffset' => 24, 'end' => '', 'endOffset' => 31],
53 ]); 88 ]);
54 $this->client->request('POST', 'annotations/'.$entry->getId().'.json', [], [], $headers, $content); 89 $this->client->request('POST', $prefixUrl.'/'.$entry->getId().'.json', [], [], $headers, $content);
55 90
56 $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); 91 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
57 92
@@ -73,22 +108,33 @@ class AnnotationControllerTest extends WallabagAnnotationTestCase
73 108
74 /** 109 /**
75 * Test editing an existing annotation. 110 * Test editing an existing annotation.
111 *
112 * @dataProvider dataForEachAnnotations
76 */ 113 */
77 public function testEditAnnotation() 114 public function testEditAnnotation($prefixUrl)
78 { 115 {
79 /** @var Annotation $annotation */ 116 $em = $this->client->getContainer()->get('doctrine.orm.entity_manager');
80 $annotation = $this->client->getContainer() 117
81 ->get('doctrine.orm.entity_manager') 118 $user = $em
82 ->getRepository('WallabagAnnotationBundle:Annotation') 119 ->getRepository('WallabagUserBundle:User')
83 ->findOneByUsername('admin'); 120 ->findOneByUserName('admin');
121 $entry = $em
122 ->getRepository('WallabagCoreBundle:Entry')
123 ->findOneByUsernameAndNotArchived('admin');
84 124
85 $this->logInAs('admin'); 125 $annotation = new Annotation($user);
126 $annotation->setEntry($entry);
127 $annotation->setText('This is my annotation /o/');
128 $annotation->setQuote('my quote');
129
130 $em->persist($annotation);
131 $em->flush();
86 132
87 $headers = ['CONTENT_TYPE' => 'application/json']; 133 $headers = ['CONTENT_TYPE' => 'application/json'];
88 $content = json_encode([ 134 $content = json_encode([
89 'text' => 'a modified annotation', 135 'text' => 'a modified annotation',
90 ]); 136 ]);
91 $this->client->request('PUT', 'annotations/'.$annotation->getId().'.json', [], [], $headers, $content); 137 $this->client->request('PUT', $prefixUrl.'/'.$annotation->getId().'.json', [], [], $headers, $content);
92 $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); 138 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
93 139
94 $content = json_decode($this->client->getResponse()->getContent(), true); 140 $content = json_decode($this->client->getResponse()->getContent(), true);
@@ -99,39 +145,55 @@ class AnnotationControllerTest extends WallabagAnnotationTestCase
99 $this->assertEquals('my quote', $content['quote']); 145 $this->assertEquals('my quote', $content['quote']);
100 146
101 /** @var Annotation $annotationUpdated */ 147 /** @var Annotation $annotationUpdated */
102 $annotationUpdated = $this->client->getContainer() 148 $annotationUpdated = $em
103 ->get('doctrine.orm.entity_manager')
104 ->getRepository('WallabagAnnotationBundle:Annotation') 149 ->getRepository('WallabagAnnotationBundle:Annotation')
105 ->findOneById($annotation->getId()); 150 ->findOneById($annotation->getId());
106 $this->assertEquals('a modified annotation', $annotationUpdated->getText()); 151 $this->assertEquals('a modified annotation', $annotationUpdated->getText());
152
153 $em->remove($annotationUpdated);
154 $em->flush();
107 } 155 }
108 156
109 /** 157 /**
110 * Test deleting an annotation. 158 * Test deleting an annotation.
159 *
160 * @dataProvider dataForEachAnnotations
111 */ 161 */
112 public function testDeleteAnnotation() 162 public function testDeleteAnnotation($prefixUrl)
113 { 163 {
114 /** @var Annotation $annotation */ 164 $em = $this->client->getContainer()->get('doctrine.orm.entity_manager');
115 $annotation = $this->client->getContainer() 165
116 ->get('doctrine.orm.entity_manager') 166 $user = $em
117 ->getRepository('WallabagAnnotationBundle:Annotation') 167 ->getRepository('WallabagUserBundle:User')
118 ->findOneByUsername('admin'); 168 ->findOneByUserName('admin');
169 $entry = $em
170 ->getRepository('WallabagCoreBundle:Entry')
171 ->findOneByUsernameAndNotArchived('admin');
119 172
120 $this->logInAs('admin'); 173 $annotation = new Annotation($user);
174 $annotation->setEntry($entry);
175 $annotation->setText('This is my annotation /o/');
176 $annotation->setQuote('my quote');
177
178 $em->persist($annotation);
179 $em->flush();
180
181 if ('annotations' === $prefixUrl) {
182 $this->logInAs('admin');
183 }
121 184
122 $headers = ['CONTENT_TYPE' => 'application/json']; 185 $headers = ['CONTENT_TYPE' => 'application/json'];
123 $content = json_encode([ 186 $content = json_encode([
124 'text' => 'a modified annotation', 187 'text' => 'a modified annotation',
125 ]); 188 ]);
126 $this->client->request('DELETE', 'annotations/'.$annotation->getId().'.json', [], [], $headers, $content); 189 $this->client->request('DELETE', $prefixUrl.'/'.$annotation->getId().'.json', [], [], $headers, $content);
127 $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); 190 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
128 191
129 $content = json_decode($this->client->getResponse()->getContent(), true); 192 $content = json_decode($this->client->getResponse()->getContent(), true);
130 193
131 $this->assertEquals('a modified annotation', $content['text']); 194 $this->assertEquals('This is my annotation /o/', $content['text']);
132 195
133 $annotationDeleted = $this->client->getContainer() 196 $annotationDeleted = $em
134 ->get('doctrine.orm.entity_manager')
135 ->getRepository('WallabagAnnotationBundle:Annotation') 197 ->getRepository('WallabagAnnotationBundle:Annotation')
136 ->findOneById($annotation->getId()); 198 ->findOneById($annotation->getId());
137 199