diff options
Diffstat (limited to 'src/Wallabag/CoreBundle')
-rw-r--r-- | src/Wallabag/CoreBundle/Helper/ContentProxy.php | 40 | ||||
-rw-r--r-- | src/Wallabag/CoreBundle/Resources/config/services.yml | 1 | ||||
-rw-r--r-- | src/Wallabag/CoreBundle/Tests/Helper/ContentProxyTest.php | 106 |
3 files changed, 142 insertions, 5 deletions
diff --git a/src/Wallabag/CoreBundle/Helper/ContentProxy.php b/src/Wallabag/CoreBundle/Helper/ContentProxy.php index bd8b993a..ba90b731 100644 --- a/src/Wallabag/CoreBundle/Helper/ContentProxy.php +++ b/src/Wallabag/CoreBundle/Helper/ContentProxy.php | |||
@@ -5,7 +5,9 @@ namespace Wallabag\CoreBundle\Helper; | |||
5 | use Graby\Graby; | 5 | use Graby\Graby; |
6 | use Psr\Log\LoggerInterface as Logger; | 6 | use Psr\Log\LoggerInterface as Logger; |
7 | use Wallabag\CoreBundle\Entity\Entry; | 7 | use Wallabag\CoreBundle\Entity\Entry; |
8 | use Wallabag\CoreBundle\Entity\Tag; | ||
8 | use Wallabag\CoreBundle\Tools\Utils; | 9 | use Wallabag\CoreBundle\Tools\Utils; |
10 | use Wallabag\CoreBundle\Repository\TagRepository; | ||
9 | 11 | ||
10 | /** | 12 | /** |
11 | * This kind of proxy class take care of getting the content from an url | 13 | * This kind of proxy class take care of getting the content from an url |
@@ -16,12 +18,14 @@ class ContentProxy | |||
16 | protected $graby; | 18 | protected $graby; |
17 | protected $tagger; | 19 | protected $tagger; |
18 | protected $logger; | 20 | protected $logger; |
21 | protected $tagRepository; | ||
19 | 22 | ||
20 | public function __construct(Graby $graby, RuleBasedTagger $tagger, Logger $logger) | 23 | public function __construct(Graby $graby, RuleBasedTagger $tagger, TagRepository $tagRepository, Logger $logger) |
21 | { | 24 | { |
22 | $this->graby = $graby; | 25 | $this->graby = $graby; |
23 | $this->tagger = $tagger; | 26 | $this->tagger = $tagger; |
24 | $this->logger = $logger; | 27 | $this->logger = $logger; |
28 | $this->tagRepository = $tagRepository; | ||
25 | } | 29 | } |
26 | 30 | ||
27 | /** | 31 | /** |
@@ -75,4 +79,38 @@ class ContentProxy | |||
75 | 79 | ||
76 | return $entry; | 80 | return $entry; |
77 | } | 81 | } |
82 | |||
83 | /** | ||
84 | * Assign some tags to an entry. | ||
85 | * | ||
86 | * @param Entry $entry | ||
87 | * @param array|string $tags An array of tag or a string coma separated of tag | ||
88 | */ | ||
89 | public function assignTagsToEntry(Entry $entry, $tags) | ||
90 | { | ||
91 | if (!is_array($tags)) { | ||
92 | $tags = explode(',', $tags); | ||
93 | } | ||
94 | |||
95 | foreach ($tags as $label) { | ||
96 | $label = trim($label); | ||
97 | |||
98 | // avoid empty tag | ||
99 | if (0 === strlen($label)) { | ||
100 | continue; | ||
101 | } | ||
102 | |||
103 | $tagEntity = $this->tagRepository->findOneByLabel($label); | ||
104 | |||
105 | if (is_null($tagEntity)) { | ||
106 | $tagEntity = new Tag(); | ||
107 | $tagEntity->setLabel($label); | ||
108 | } | ||
109 | |||
110 | // only add the tag on the entry if the relation doesn't exist | ||
111 | if (false === $entry->getTags()->contains($tagEntity)) { | ||
112 | $entry->addTag($tagEntity); | ||
113 | } | ||
114 | } | ||
115 | } | ||
78 | } | 116 | } |
diff --git a/src/Wallabag/CoreBundle/Resources/config/services.yml b/src/Wallabag/CoreBundle/Resources/config/services.yml index a8796fe4..1aa66be1 100644 --- a/src/Wallabag/CoreBundle/Resources/config/services.yml +++ b/src/Wallabag/CoreBundle/Resources/config/services.yml | |||
@@ -50,6 +50,7 @@ services: | |||
50 | arguments: | 50 | arguments: |
51 | - "@wallabag_core.graby" | 51 | - "@wallabag_core.graby" |
52 | - "@wallabag_core.rule_based_tagger" | 52 | - "@wallabag_core.rule_based_tagger" |
53 | - "@wallabag_core.tag_repository" | ||
53 | - "@logger" | 54 | - "@logger" |
54 | 55 | ||
55 | wallabag_core.rule_based_tagger: | 56 | wallabag_core.rule_based_tagger: |
diff --git a/src/Wallabag/CoreBundle/Tests/Helper/ContentProxyTest.php b/src/Wallabag/CoreBundle/Tests/Helper/ContentProxyTest.php index d29984e9..f58b5828 100644 --- a/src/Wallabag/CoreBundle/Tests/Helper/ContentProxyTest.php +++ b/src/Wallabag/CoreBundle/Tests/Helper/ContentProxyTest.php | |||
@@ -3,8 +3,9 @@ | |||
3 | namespace Wallabag\CoreBundle\Tests\Helper; | 3 | namespace Wallabag\CoreBundle\Tests\Helper; |
4 | 4 | ||
5 | use Psr\Log\NullLogger; | 5 | use Psr\Log\NullLogger; |
6 | use Wallabag\CoreBundle\Entity\Entry; | ||
7 | use Wallabag\CoreBundle\Helper\ContentProxy; | 6 | use Wallabag\CoreBundle\Helper\ContentProxy; |
7 | use Wallabag\CoreBundle\Entity\Entry; | ||
8 | use Wallabag\CoreBundle\Entity\Tag; | ||
8 | use Wallabag\UserBundle\Entity\User; | 9 | use Wallabag\UserBundle\Entity\User; |
9 | 10 | ||
10 | class ContentProxyTest extends \PHPUnit_Framework_TestCase | 11 | class ContentProxyTest extends \PHPUnit_Framework_TestCase |
@@ -30,7 +31,7 @@ class ContentProxyTest extends \PHPUnit_Framework_TestCase | |||
30 | 'language' => '', | 31 | 'language' => '', |
31 | )); | 32 | )); |
32 | 33 | ||
33 | $proxy = new ContentProxy($graby, $tagger, $this->getLogger()); | 34 | $proxy = new ContentProxy($graby, $tagger, $this->getTagRepositoryMock(), $this->getLogger()); |
34 | $entry = $proxy->updateEntry(new Entry(new User()), 'http://0.0.0.0'); | 35 | $entry = $proxy->updateEntry(new Entry(new User()), 'http://0.0.0.0'); |
35 | 36 | ||
36 | $this->assertEquals('http://0.0.0.0', $entry->getUrl()); | 37 | $this->assertEquals('http://0.0.0.0', $entry->getUrl()); |
@@ -68,7 +69,7 @@ class ContentProxyTest extends \PHPUnit_Framework_TestCase | |||
68 | ), | 69 | ), |
69 | )); | 70 | )); |
70 | 71 | ||
71 | $proxy = new ContentProxy($graby, $tagger, $this->getLogger()); | 72 | $proxy = new ContentProxy($graby, $tagger, $this->getTagRepositoryMock(), $this->getLogger()); |
72 | $entry = $proxy->updateEntry(new Entry(new User()), 'http://domain.io'); | 73 | $entry = $proxy->updateEntry(new Entry(new User()), 'http://domain.io'); |
73 | 74 | ||
74 | $this->assertEquals('http://domain.io', $entry->getUrl()); | 75 | $this->assertEquals('http://domain.io', $entry->getUrl()); |
@@ -107,7 +108,7 @@ class ContentProxyTest extends \PHPUnit_Framework_TestCase | |||
107 | ), | 108 | ), |
108 | )); | 109 | )); |
109 | 110 | ||
110 | $proxy = new ContentProxy($graby, $tagger, $this->getLogger()); | 111 | $proxy = new ContentProxy($graby, $tagger, $this->getTagRepositoryMock(), $this->getLogger()); |
111 | $entry = $proxy->updateEntry(new Entry(new User()), 'http://0.0.0.0'); | 112 | $entry = $proxy->updateEntry(new Entry(new User()), 'http://0.0.0.0'); |
112 | 113 | ||
113 | $this->assertEquals('http://1.1.1.1', $entry->getUrl()); | 114 | $this->assertEquals('http://1.1.1.1', $entry->getUrl()); |
@@ -120,6 +121,96 @@ class ContentProxyTest extends \PHPUnit_Framework_TestCase | |||
120 | $this->assertEquals('1.1.1.1', $entry->getDomainName()); | 121 | $this->assertEquals('1.1.1.1', $entry->getDomainName()); |
121 | } | 122 | } |
122 | 123 | ||
124 | public function testAssignTagsWithArrayAndExtraSpaces() | ||
125 | { | ||
126 | $graby = $this->getMockBuilder('Graby\Graby') | ||
127 | ->disableOriginalConstructor() | ||
128 | ->getMock(); | ||
129 | |||
130 | $tagRepo = $this->getTagRepositoryMock(); | ||
131 | $proxy = new ContentProxy($graby, $this->getTaggerMock(), $tagRepo, $this->getLogger()); | ||
132 | |||
133 | $entry = new Entry(new User()); | ||
134 | |||
135 | $proxy->assignTagsToEntry($entry, array(' tag1', 'tag2 ')); | ||
136 | |||
137 | $this->assertCount(2, $entry->getTags()); | ||
138 | $this->assertEquals('tag1', $entry->getTags()[0]->getLabel()); | ||
139 | $this->assertEquals('tag2', $entry->getTags()[1]->getLabel()); | ||
140 | } | ||
141 | |||
142 | public function testAssignTagsWithString() | ||
143 | { | ||
144 | $graby = $this->getMockBuilder('Graby\Graby') | ||
145 | ->disableOriginalConstructor() | ||
146 | ->getMock(); | ||
147 | |||
148 | $tagRepo = $this->getTagRepositoryMock(); | ||
149 | $proxy = new ContentProxy($graby, $this->getTaggerMock(), $tagRepo, $this->getLogger()); | ||
150 | |||
151 | $entry = new Entry(new User()); | ||
152 | |||
153 | $proxy->assignTagsToEntry($entry, 'tag1, tag2'); | ||
154 | |||
155 | $this->assertCount(2, $entry->getTags()); | ||
156 | $this->assertEquals('tag1', $entry->getTags()[0]->getLabel()); | ||
157 | $this->assertEquals('tag2', $entry->getTags()[1]->getLabel()); | ||
158 | } | ||
159 | |||
160 | public function testAssignTagsWithEmptyArray() | ||
161 | { | ||
162 | $graby = $this->getMockBuilder('Graby\Graby') | ||
163 | ->disableOriginalConstructor() | ||
164 | ->getMock(); | ||
165 | |||
166 | $tagRepo = $this->getTagRepositoryMock(); | ||
167 | $proxy = new ContentProxy($graby, $this->getTaggerMock(), $tagRepo, $this->getLogger()); | ||
168 | |||
169 | $entry = new Entry(new User()); | ||
170 | |||
171 | $proxy->assignTagsToEntry($entry, array()); | ||
172 | |||
173 | $this->assertCount(0, $entry->getTags()); | ||
174 | } | ||
175 | |||
176 | public function testAssignTagsWithEmptyString() | ||
177 | { | ||
178 | $graby = $this->getMockBuilder('Graby\Graby') | ||
179 | ->disableOriginalConstructor() | ||
180 | ->getMock(); | ||
181 | |||
182 | $tagRepo = $this->getTagRepositoryMock(); | ||
183 | $proxy = new ContentProxy($graby, $this->getTaggerMock(), $tagRepo, $this->getLogger()); | ||
184 | |||
185 | $entry = new Entry(new User()); | ||
186 | |||
187 | $proxy->assignTagsToEntry($entry, ''); | ||
188 | |||
189 | $this->assertCount(0, $entry->getTags()); | ||
190 | } | ||
191 | |||
192 | public function testAssignTagsAlreadyAssigned() | ||
193 | { | ||
194 | $graby = $this->getMockBuilder('Graby\Graby') | ||
195 | ->disableOriginalConstructor() | ||
196 | ->getMock(); | ||
197 | |||
198 | $tagRepo = $this->getTagRepositoryMock(); | ||
199 | $proxy = new ContentProxy($graby, $this->getTaggerMock(), $tagRepo, $this->getLogger()); | ||
200 | |||
201 | $tagEntity = new Tag(); | ||
202 | $tagEntity->setLabel('tag1'); | ||
203 | |||
204 | $entry = new Entry(new User()); | ||
205 | $entry->addTag($tagEntity); | ||
206 | |||
207 | $proxy->assignTagsToEntry($entry, 'tag1, tag2'); | ||
208 | |||
209 | $this->assertCount(2, $entry->getTags()); | ||
210 | $this->assertEquals('tag1', $entry->getTags()[0]->getLabel()); | ||
211 | $this->assertEquals('tag2', $entry->getTags()[1]->getLabel()); | ||
212 | } | ||
213 | |||
123 | private function getTaggerMock() | 214 | private function getTaggerMock() |
124 | { | 215 | { |
125 | return $this->getMockBuilder('Wallabag\CoreBundle\Helper\RuleBasedTagger') | 216 | return $this->getMockBuilder('Wallabag\CoreBundle\Helper\RuleBasedTagger') |
@@ -128,6 +219,13 @@ class ContentProxyTest extends \PHPUnit_Framework_TestCase | |||
128 | ->getMock(); | 219 | ->getMock(); |
129 | } | 220 | } |
130 | 221 | ||
222 | private function getTagRepositoryMock() | ||
223 | { | ||
224 | return $this->getMockBuilder('Wallabag\CoreBundle\Repository\TagRepository') | ||
225 | ->disableOriginalConstructor() | ||
226 | ->getMock(); | ||
227 | } | ||
228 | |||
131 | private function getLogger() | 229 | private function getLogger() |
132 | { | 230 | { |
133 | return new NullLogger(); | 231 | return new NullLogger(); |