aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorJeremy Benoist <jeremy.benoist@gmail.com>2017-07-03 13:56:39 +0200
committerJeremy Benoist <jeremy.benoist@gmail.com>2017-07-03 13:56:39 +0200
commitc18a2476b601bc6b9893462d9be680c2e13c89e8 (patch)
treee1e2d162251b7069236ce66bc57694695d5571bb
parentd0ec2ddd2354e39badd947c2214f47193784b1c7 (diff)
downloadwallabag-c18a2476b601bc6b9893462d9be680c2e13c89e8.tar.gz
wallabag-c18a2476b601bc6b9893462d9be680c2e13c89e8.tar.zst
wallabag-c18a2476b601bc6b9893462d9be680c2e13c89e8.zip
CS
-rw-r--r--src/Wallabag/ApiBundle/Controller/EntryRestController.php12
-rw-r--r--src/Wallabag/CoreBundle/Helper/ContentProxy.php140
-rw-r--r--tests/Wallabag/ApiBundle/Controller/EntryRestControllerTest.php29
-rw-r--r--tests/Wallabag/CoreBundle/Helper/ContentProxyTest.php10
4 files changed, 95 insertions, 96 deletions
diff --git a/src/Wallabag/ApiBundle/Controller/EntryRestController.php b/src/Wallabag/ApiBundle/Controller/EntryRestController.php
index a2e913af..8a206124 100644
--- a/src/Wallabag/ApiBundle/Controller/EntryRestController.php
+++ b/src/Wallabag/ApiBundle/Controller/EntryRestController.php
@@ -354,11 +354,11 @@ class EntryRestController extends WallabagRestController
354 ]); 354 ]);
355 } 355 }
356 356
357 if (!is_null($data['isArchived'])) { 357 if (null !== $data['isArchived']) {
358 $entry->setArchived((bool) $data['isArchived']); 358 $entry->setArchived((bool) $data['isArchived']);
359 } 359 }
360 360
361 if (!is_null($data['isStarred'])) { 361 if (null !== $data['isStarred']) {
362 $entry->setStarred((bool) $data['isStarred']); 362 $entry->setStarred((bool) $data['isStarred']);
363 } 363 }
364 364
@@ -366,7 +366,7 @@ class EntryRestController extends WallabagRestController
366 $this->get('wallabag_core.tags_assigner')->assignTagsToEntry($entry, $data['tags']); 366 $this->get('wallabag_core.tags_assigner')->assignTagsToEntry($entry, $data['tags']);
367 } 367 }
368 368
369 if (!is_null($data['isPublic'])) { 369 if (null !== $data['isPublic']) {
370 if (true === (bool) $data['isPublic'] && null === $entry->getUid()) { 370 if (true === (bool) $data['isPublic'] && null === $entry->getUid()) {
371 $entry->generateUid(); 371 $entry->generateUid();
372 } elseif (false === (bool) $data['isPublic']) { 372 } elseif (false === (bool) $data['isPublic']) {
@@ -457,11 +457,11 @@ class EntryRestController extends WallabagRestController
457 $contentProxy->updatePublishedAt($entry, $data['publishedAt']); 457 $contentProxy->updatePublishedAt($entry, $data['publishedAt']);
458 } 458 }
459 459
460 if (!is_null($data['isArchived'])) { 460 if (null !== $data['isArchived']) {
461 $entry->setArchived((bool) $data['isArchived']); 461 $entry->setArchived((bool) $data['isArchived']);
462 } 462 }
463 463
464 if (!is_null($data['isStarred'])) { 464 if (null !== $data['isStarred']) {
465 $entry->setStarred((bool) $data['isStarred']); 465 $entry->setStarred((bool) $data['isStarred']);
466 } 466 }
467 467
@@ -470,7 +470,7 @@ class EntryRestController extends WallabagRestController
470 $this->get('wallabag_core.tags_assigner')->assignTagsToEntry($entry, $data['tags']); 470 $this->get('wallabag_core.tags_assigner')->assignTagsToEntry($entry, $data['tags']);
471 } 471 }
472 472
473 if (!is_null($data['isPublic'])) { 473 if (null !== $data['isPublic']) {
474 if (true === (bool) $data['isPublic'] && null === $entry->getUid()) { 474 if (true === (bool) $data['isPublic'] && null === $entry->getUid()) {
475 $entry->generateUid(); 475 $entry->generateUid();
476 } elseif (false === (bool) $data['isPublic']) { 476 } elseif (false === (bool) $data['isPublic']) {
diff --git a/src/Wallabag/CoreBundle/Helper/ContentProxy.php b/src/Wallabag/CoreBundle/Helper/ContentProxy.php
index 5622cc83..656ac6ee 100644
--- a/src/Wallabag/CoreBundle/Helper/ContentProxy.php
+++ b/src/Wallabag/CoreBundle/Helper/ContentProxy.php
@@ -67,6 +67,76 @@ class ContentProxy
67 } 67 }
68 68
69 /** 69 /**
70 * Use a Symfony validator to ensure the language is well formatted.
71 *
72 * @param Entry $entry
73 * @param string $value Language to validate and save
74 */
75 public function updateLanguage(Entry $entry, $value)
76 {
77 // some lang are defined as fr-FR, es-ES.
78 // replacing - by _ might increase language support
79 $value = str_replace('-', '_', $value);
80
81 $errors = $this->validator->validate(
82 $value,
83 (new LocaleConstraint())
84 );
85
86 if (0 === count($errors)) {
87 $entry->setLanguage($value);
88
89 return;
90 }
91
92 $this->logger->warning('Language validation failed. ' . (string) $errors);
93 }
94
95 /**
96 * Use a Symfony validator to ensure the preview picture is a real url.
97 *
98 * @param Entry $entry
99 * @param string $value URL to validate and save
100 */
101 public function updatePreviewPicture(Entry $entry, $value)
102 {
103 $errors = $this->validator->validate(
104 $value,
105 (new UrlConstraint())
106 );
107
108 if (0 === count($errors)) {
109 $entry->setPreviewPicture($value);
110
111 return;
112 }
113
114 $this->logger->warning('PreviewPicture validation failed. ' . (string) $errors);
115 }
116
117 /**
118 * Update date.
119 *
120 * @param Entry $entry
121 * @param string $value Date to validate and save
122 */
123 public function updatePublishedAt(Entry $entry, $value)
124 {
125 $date = $value;
126
127 // is it a timestamp?
128 if (filter_var($date, FILTER_VALIDATE_INT) !== false) {
129 $date = '@' . $value;
130 }
131
132 try {
133 $entry->setPublishedAt(new \DateTime($date));
134 } catch (\Exception $e) {
135 $this->logger->warning('Error while defining date', ['e' => $e, 'url' => $entry->getUrl(), 'date' => $value]);
136 }
137 }
138
139 /**
70 * Stock entry with fetched or imported content. 140 * Stock entry with fetched or imported content.
71 * Will fall back to OpenGraph data if available. 141 * Will fall back to OpenGraph data if available.
72 * 142 *
@@ -155,74 +225,4 @@ class ContentProxy
155 { 225 {
156 return !empty($content['title']) && !empty($content['html']) && !empty($content['url']); 226 return !empty($content['title']) && !empty($content['html']) && !empty($content['url']);
157 } 227 }
158
159 /**
160 * Use a Symfony validator to ensure the language is well formatted.
161 *
162 * @param Entry $entry
163 * @param string $value Language to validate and save
164 */
165 public function updateLanguage(Entry $entry, $value)
166 {
167 // some lang are defined as fr-FR, es-ES.
168 // replacing - by _ might increase language support
169 $value = str_replace('-', '_', $value);
170
171 $errors = $this->validator->validate(
172 $value,
173 (new LocaleConstraint())
174 );
175
176 if (0 === count($errors)) {
177 $entry->setLanguage($value);
178
179 return;
180 }
181
182 $this->logger->warning('Language validation failed. ' . (string) $errors);
183 }
184
185 /**
186 * Use a Symfony validator to ensure the preview picture is a real url.
187 *
188 * @param Entry $entry
189 * @param string $value URL to validate and save
190 */
191 public function updatePreviewPicture(Entry $entry, $value)
192 {
193 $errors = $this->validator->validate(
194 $value,
195 (new UrlConstraint())
196 );
197
198 if (0 === count($errors)) {
199 $entry->setPreviewPicture($value);
200
201 return;
202 }
203
204 $this->logger->warning('PreviewPicture validation failed. ' . (string) $errors);
205 }
206
207 /**
208 * Update date.
209 *
210 * @param Entry $entry
211 * @param string $value Date to validate and save
212 */
213 public function updatePublishedAt(Entry $entry, $value)
214 {
215 $date = $value;
216
217 // is it a timestamp?
218 if (filter_var($date, FILTER_VALIDATE_INT) !== false) {
219 $date = '@'.$value;
220 }
221
222 try {
223 $entry->setPublishedAt(new \DateTime($date));
224 } catch (\Exception $e) {
225 $this->logger->warning('Error while defining date', ['e' => $e, 'url' => $entry->getUrl(), 'date' => $value]);
226 }
227 }
228} 228}
diff --git a/tests/Wallabag/ApiBundle/Controller/EntryRestControllerTest.php b/tests/Wallabag/ApiBundle/Controller/EntryRestControllerTest.php
index 0647bb23..c76be13d 100644
--- a/tests/Wallabag/ApiBundle/Controller/EntryRestControllerTest.php
+++ b/tests/Wallabag/ApiBundle/Controller/EntryRestControllerTest.php
@@ -519,7 +519,7 @@ class EntryRestControllerTest extends WallabagApiTestCase
519 $this->markTestSkipped('No content found in db.'); 519 $this->markTestSkipped('No content found in db.');
520 } 520 }
521 521
522 $this->client->request('PATCH', '/api/entries/'.$entry->getId().'.json', [ 522 $this->client->request('PATCH', '/api/entries/' . $entry->getId() . '.json', [
523 'title' => 'New awesome title', 523 'title' => 'New awesome title',
524 'tags' => 'new tag ' . uniqid(), 524 'tags' => 'new tag ' . uniqid(),
525 'starred' => '1', 525 'starred' => '1',
@@ -579,10 +579,9 @@ class EntryRestControllerTest extends WallabagApiTestCase
579 $this->assertSame($entry->getId(), $content['id']); 579 $this->assertSame($entry->getId(), $content['id']);
580 $this->assertSame($entry->getUrl(), $content['url']); 580 $this->assertSame($entry->getUrl(), $content['url']);
581 $this->assertGreaterThanOrEqual(1, count($content['tags']), 'We force only one tag'); 581 $this->assertGreaterThanOrEqual(1, count($content['tags']), 'We force only one tag');
582 $this->assertGreaterThan($nbTags, count($content['tags']));
583 $this->assertEmpty($content['published_by'], 'Authors were not saved because of an array instead of a string'); 582 $this->assertEmpty($content['published_by'], 'Authors were not saved because of an array instead of a string');
584 $this->assertEquals($previousContent, $content['content'], 'Ensure content has not moved'); 583 $this->assertSame($previousContent, $content['content'], 'Ensure content has not moved');
585 $this->assertEquals($previousLanguage, $content['language'], 'Ensure language has not moved'); 584 $this->assertSame($previousLanguage, $content['language'], 'Ensure language has not moved');
586 } 585 }
587 586
588 public function testGetTagsEntry() 587 public function testGetTagsEntry()
@@ -730,8 +729,8 @@ class EntryRestControllerTest extends WallabagApiTestCase
730 729
731 $previousTitle = $entry->getTitle(); 730 $previousTitle = $entry->getTitle();
732 731
733 $this->client->request('PATCH', '/api/entries/'.$entry->getId().'.json', [ 732 $this->client->request('PATCH', '/api/entries/' . $entry->getId() . '.json', [
734 'title' => $entry->getTitle().'++', 733 'title' => $entry->getTitle() . '++',
735 ]); 734 ]);
736 735
737 $this->assertSame(200, $this->client->getResponse()->getStatusCode()); 736 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
@@ -739,7 +738,7 @@ class EntryRestControllerTest extends WallabagApiTestCase
739 $content = json_decode($this->client->getResponse()->getContent(), true); 738 $content = json_decode($this->client->getResponse()->getContent(), true);
740 739
741 $this->assertSame(1, $content['is_archived']); 740 $this->assertSame(1, $content['is_archived']);
742 $this->assertEquals($previousTitle.'++', $content['title']); 741 $this->assertSame($previousTitle . '++', $content['title']);
743 } 742 }
744 743
745 public function testSaveIsStarredAfterPatch() 744 public function testSaveIsStarredAfterPatch()
@@ -913,9 +912,9 @@ class EntryRestControllerTest extends WallabagApiTestCase
913 912
914 public function testPostEntriesTagsListActionNoList() 913 public function testPostEntriesTagsListActionNoList()
915 { 914 {
916 $this->client->request('POST', '/api/entries/tags/lists?list='.json_encode([])); 915 $this->client->request('POST', '/api/entries/tags/lists?list=' . json_encode([]));
917 916
918 $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); 917 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
919 918
920 $content = json_decode($this->client->getResponse()->getContent(), true); 919 $content = json_decode($this->client->getResponse()->getContent(), true);
921 920
@@ -950,9 +949,9 @@ class EntryRestControllerTest extends WallabagApiTestCase
950 949
951 public function testDeleteEntriesTagsListActionNoList() 950 public function testDeleteEntriesTagsListActionNoList()
952 { 951 {
953 $this->client->request('DELETE', '/api/entries/tags/list?list='.json_encode([])); 952 $this->client->request('DELETE', '/api/entries/tags/list?list=' . json_encode([]));
954 953
955 $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); 954 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
956 955
957 $content = json_decode($this->client->getResponse()->getContent(), true); 956 $content = json_decode($this->client->getResponse()->getContent(), true);
958 957
@@ -981,9 +980,9 @@ class EntryRestControllerTest extends WallabagApiTestCase
981 980
982 public function testPostEntriesListActionWithNoUrls() 981 public function testPostEntriesListActionWithNoUrls()
983 { 982 {
984 $this->client->request('POST', '/api/entries/lists?urls='.json_encode([])); 983 $this->client->request('POST', '/api/entries/lists?urls=' . json_encode([]));
985 984
986 $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); 985 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
987 986
988 $content = json_decode($this->client->getResponse()->getContent(), true); 987 $content = json_decode($this->client->getResponse()->getContent(), true);
989 988
@@ -1017,9 +1016,9 @@ class EntryRestControllerTest extends WallabagApiTestCase
1017 1016
1018 public function testDeleteEntriesListActionWithNoUrls() 1017 public function testDeleteEntriesListActionWithNoUrls()
1019 { 1018 {
1020 $this->client->request('DELETE', '/api/entries/list?urls='.json_encode([])); 1019 $this->client->request('DELETE', '/api/entries/list?urls=' . json_encode([]));
1021 1020
1022 $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); 1021 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
1023 1022
1024 $content = json_decode($this->client->getResponse()->getContent(), true); 1023 $content = json_decode($this->client->getResponse()->getContent(), true);
1025 1024
diff --git a/tests/Wallabag/CoreBundle/Helper/ContentProxyTest.php b/tests/Wallabag/CoreBundle/Helper/ContentProxyTest.php
index c0b68d53..f94c2137 100644
--- a/tests/Wallabag/CoreBundle/Helper/ContentProxyTest.php
+++ b/tests/Wallabag/CoreBundle/Helper/ContentProxyTest.php
@@ -521,13 +521,13 @@ class ContentProxyTest extends \PHPUnit_Framework_TestCase
521 $entry = new Entry(new User()); 521 $entry = new Entry(new User());
522 $proxy->updateEntry($entry, 'http://0.0.0.0'); 522 $proxy->updateEntry($entry, 'http://0.0.0.0');
523 523
524 $this->assertEquals('http://1.1.1.1/image.jpg', $entry->getUrl()); 524 $this->assertSame('http://1.1.1.1/image.jpg', $entry->getUrl());
525 $this->assertEquals('this is my title', $entry->getTitle()); 525 $this->assertSame('this is my title', $entry->getTitle());
526 $this->assertContains('http://1.1.1.1/image.jpg', $entry->getContent()); 526 $this->assertContains('http://1.1.1.1/image.jpg', $entry->getContent());
527 $this->assertSame('http://1.1.1.1/image.jpg', $entry->getPreviewPicture()); 527 $this->assertSame('http://1.1.1.1/image.jpg', $entry->getPreviewPicture());
528 $this->assertEquals('image/jpeg', $entry->getMimetype()); 528 $this->assertSame('image/jpeg', $entry->getMimetype());
529 $this->assertEquals('200', $entry->getHttpStatus()); 529 $this->assertSame('200', $entry->getHttpStatus());
530 $this->assertEquals('1.1.1.1', $entry->getDomainName()); 530 $this->assertSame('1.1.1.1', $entry->getDomainName());
531 } 531 }
532 532
533 private function getTaggerMock() 533 private function getTaggerMock()