aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests/Wallabag
diff options
context:
space:
mode:
authorJeremy Benoist <j0k3r@users.noreply.github.com>2016-09-26 14:47:02 +0200
committerGitHub <noreply@github.com>2016-09-26 14:47:02 +0200
commitd6de23a100221ae1afaa92a58af17a17d0c6614e (patch)
treebd105198c75ea6b8e5d37a80a9f0135942a1c5eb /tests/Wallabag
parent7e98ad962680fac17b3b90ae34b9c6e5afe7636f (diff)
parentfefef9d41b4d1bd9efbd49011159bae70bf67528 (diff)
downloadwallabag-d6de23a100221ae1afaa92a58af17a17d0c6614e.tar.gz
wallabag-d6de23a100221ae1afaa92a58af17a17d0c6614e.tar.zst
wallabag-d6de23a100221ae1afaa92a58af17a17d0c6614e.zip
Merge pull request #2192 from wallabag/import-browser-bookmarks
Import Firefox & Chrome bookmarks into wallabag
Diffstat (limited to 'tests/Wallabag')
-rw-r--r--tests/Wallabag/ApiBundle/Controller/WallabagRestControllerTest.php2
-rw-r--r--tests/Wallabag/ImportBundle/Command/ImportCommandTest.php1
-rw-r--r--tests/Wallabag/ImportBundle/Controller/ChromeControllerTest.php152
-rw-r--r--tests/Wallabag/ImportBundle/Controller/FirefoxControllerTest.php165
-rw-r--r--tests/Wallabag/ImportBundle/Controller/ImportControllerTest.php2
-rw-r--r--tests/Wallabag/ImportBundle/Controller/ReadabilityControllerTest.php1
-rw-r--r--tests/Wallabag/ImportBundle/Import/ChromeImportTest.php233
-rw-r--r--tests/Wallabag/ImportBundle/Import/FirefoxImportTest.php233
-rw-r--r--tests/Wallabag/ImportBundle/fixtures/chrome-bookmarks36
-rw-r--r--tests/Wallabag/ImportBundle/fixtures/firefox-bookmarks.json63
10 files changed, 884 insertions, 4 deletions
diff --git a/tests/Wallabag/ApiBundle/Controller/WallabagRestControllerTest.php b/tests/Wallabag/ApiBundle/Controller/WallabagRestControllerTest.php
index ee5b2ab7..101c20eb 100644
--- a/tests/Wallabag/ApiBundle/Controller/WallabagRestControllerTest.php
+++ b/tests/Wallabag/ApiBundle/Controller/WallabagRestControllerTest.php
@@ -80,7 +80,7 @@ class WallabagRestControllerTest extends WallabagApiTestCase
80 80
81 public function testGetStarredEntries() 81 public function testGetStarredEntries()
82 { 82 {
83 $this->client->request('GET', '/api/entries', ['star' => 1, 'sort' => 'updated']); 83 $this->client->request('GET', '/api/entries', ['starred' => 1, 'sort' => 'updated']);
84 84
85 $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); 85 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
86 86
diff --git a/tests/Wallabag/ImportBundle/Command/ImportCommandTest.php b/tests/Wallabag/ImportBundle/Command/ImportCommandTest.php
index eb7fce79..7be1eb18 100644
--- a/tests/Wallabag/ImportBundle/Command/ImportCommandTest.php
+++ b/tests/Wallabag/ImportBundle/Command/ImportCommandTest.php
@@ -6,7 +6,6 @@ use Symfony\Bundle\FrameworkBundle\Console\Application;
6use Symfony\Component\Console\Tester\CommandTester; 6use Symfony\Component\Console\Tester\CommandTester;
7use Wallabag\ImportBundle\Command\ImportCommand; 7use Wallabag\ImportBundle\Command\ImportCommand;
8use Tests\Wallabag\CoreBundle\WallabagCoreTestCase; 8use Tests\Wallabag\CoreBundle\WallabagCoreTestCase;
9use M6Web\Component\RedisMock\RedisMockFactory;
10 9
11class ImportCommandTest extends WallabagCoreTestCase 10class ImportCommandTest extends WallabagCoreTestCase
12{ 11{
diff --git a/tests/Wallabag/ImportBundle/Controller/ChromeControllerTest.php b/tests/Wallabag/ImportBundle/Controller/ChromeControllerTest.php
new file mode 100644
index 00000000..23c80bec
--- /dev/null
+++ b/tests/Wallabag/ImportBundle/Controller/ChromeControllerTest.php
@@ -0,0 +1,152 @@
1<?php
2
3namespace Tests\Wallabag\ImportBundle\Controller;
4
5use Tests\Wallabag\CoreBundle\WallabagCoreTestCase;
6use Symfony\Component\HttpFoundation\File\UploadedFile;
7
8class ChromeControllerTest extends WallabagCoreTestCase
9{
10 public function testImportChrome()
11 {
12 $this->logInAs('admin');
13 $client = $this->getClient();
14
15 $crawler = $client->request('GET', '/import/chrome');
16
17 $this->assertEquals(200, $client->getResponse()->getStatusCode());
18 $this->assertEquals(1, $crawler->filter('form[name=upload_import_file] > button[type=submit]')->count());
19 $this->assertEquals(1, $crawler->filter('input[type=file]')->count());
20 }
21
22 public function testImportChromeWithRabbitEnabled()
23 {
24 $this->logInAs('admin');
25 $client = $this->getClient();
26
27 $client->getContainer()->get('craue_config')->set('import_with_rabbitmq', 1);
28
29 $crawler = $client->request('GET', '/import/chrome');
30
31 $this->assertEquals(200, $client->getResponse()->getStatusCode());
32 $this->assertEquals(1, $crawler->filter('form[name=upload_import_file] > button[type=submit]')->count());
33 $this->assertEquals(1, $crawler->filter('input[type=file]')->count());
34
35 $client->getContainer()->get('craue_config')->set('import_with_rabbitmq', 0);
36 }
37
38 public function testImportChromeBadFile()
39 {
40 $this->logInAs('admin');
41 $client = $this->getClient();
42
43 $crawler = $client->request('GET', '/import/chrome');
44 $form = $crawler->filter('form[name=upload_import_file] > button[type=submit]')->form();
45
46 $data = [
47 'upload_import_file[file]' => '',
48 ];
49
50 $client->submit($form, $data);
51
52 $this->assertEquals(200, $client->getResponse()->getStatusCode());
53 }
54
55 public function testImportChromeWithRedisEnabled()
56 {
57 $this->logInAs('admin');
58 $client = $this->getClient();
59 $client->getContainer()->get('craue_config')->set('import_with_redis', 1);
60
61 $crawler = $client->request('GET', '/import/chrome');
62
63 $this->assertEquals(200, $client->getResponse()->getStatusCode());
64 $this->assertEquals(1, $crawler->filter('form[name=upload_import_file] > button[type=submit]')->count());
65 $this->assertEquals(1, $crawler->filter('input[type=file]')->count());
66
67 $form = $crawler->filter('form[name=upload_import_file] > button[type=submit]')->form();
68
69 $file = new UploadedFile(__DIR__.'/../fixtures/chrome-bookmarks', 'Bookmarks');
70
71 $data = [
72 'upload_import_file[file]' => $file,
73 ];
74
75 $client->submit($form, $data);
76
77 $this->assertEquals(302, $client->getResponse()->getStatusCode());
78
79 $crawler = $client->followRedirect();
80
81 $this->assertGreaterThan(1, $body = $crawler->filter('body')->extract(['_text']));
82 $this->assertContains('flashes.import.notice.summary', $body[0]);
83
84 $this->assertNotEmpty($client->getContainer()->get('wallabag_core.redis.client')->lpop('wallabag.import.chrome'));
85
86 $client->getContainer()->get('craue_config')->set('import_with_redis', 0);
87 }
88
89 public function testImportWallabagWithChromeFile()
90 {
91 $this->logInAs('admin');
92 $client = $this->getClient();
93
94 $crawler = $client->request('GET', '/import/chrome');
95 $form = $crawler->filter('form[name=upload_import_file] > button[type=submit]')->form();
96
97 $file = new UploadedFile(__DIR__.'/../fixtures/chrome-bookmarks', 'Bookmarks');
98
99 $data = [
100 'upload_import_file[file]' => $file,
101 ];
102
103 $client->submit($form, $data);
104
105 $this->assertEquals(302, $client->getResponse()->getStatusCode());
106
107 $crawler = $client->followRedirect();
108
109 $this->assertGreaterThan(1, $body = $crawler->filter('body')->extract(['_text']));
110 $this->assertContains('flashes.import.notice.summary', $body[0]);
111
112 $content = $client->getContainer()
113 ->get('doctrine.orm.entity_manager')
114 ->getRepository('WallabagCoreBundle:Entry')
115 ->findByUrlAndUserId(
116 'http://www.usinenouvelle.com/article/la-multiplication-des-chefs-de-projet-est-une-catastrophe-manageriale-majeure-affirme-le-sociologue-francois-dupuy.N307730',
117 $this->getLoggedInUserId()
118 );
119
120 $this->assertNotEmpty($content->getPreviewPicture());
121 $this->assertNotEmpty($content->getLanguage());
122 $this->assertEquals(0, count($content->getTags()));
123
124 $createdAt = $content->getCreatedAt();
125 $this->assertEquals('2011', $createdAt->format('Y'));
126 $this->assertEquals('07', $createdAt->format('m'));
127 }
128
129 public function testImportWallabagWithEmptyFile()
130 {
131 $this->logInAs('admin');
132 $client = $this->getClient();
133
134 $crawler = $client->request('GET', '/import/chrome');
135 $form = $crawler->filter('form[name=upload_import_file] > button[type=submit]')->form();
136
137 $file = new UploadedFile(__DIR__.'/../fixtures/test.txt', 'test.txt');
138
139 $data = [
140 'upload_import_file[file]' => $file,
141 ];
142
143 $client->submit($form, $data);
144
145 $this->assertEquals(302, $client->getResponse()->getStatusCode());
146
147 $crawler = $client->followRedirect();
148
149 $this->assertGreaterThan(1, $body = $crawler->filter('body')->extract(['_text']));
150 $this->assertContains('flashes.import.notice.failed', $body[0]);
151 }
152}
diff --git a/tests/Wallabag/ImportBundle/Controller/FirefoxControllerTest.php b/tests/Wallabag/ImportBundle/Controller/FirefoxControllerTest.php
new file mode 100644
index 00000000..98f13d72
--- /dev/null
+++ b/tests/Wallabag/ImportBundle/Controller/FirefoxControllerTest.php
@@ -0,0 +1,165 @@
1<?php
2
3namespace Tests\Wallabag\ImportBundle\Controller;
4
5use Tests\Wallabag\CoreBundle\WallabagCoreTestCase;
6use Symfony\Component\HttpFoundation\File\UploadedFile;
7
8class FirefoxControllerTest extends WallabagCoreTestCase
9{
10 public function testImportFirefox()
11 {
12 $this->logInAs('admin');
13 $client = $this->getClient();
14
15 $crawler = $client->request('GET', '/import/firefox');
16
17 $this->assertEquals(200, $client->getResponse()->getStatusCode());
18 $this->assertEquals(1, $crawler->filter('form[name=upload_import_file] > button[type=submit]')->count());
19 $this->assertEquals(1, $crawler->filter('input[type=file]')->count());
20 }
21
22 public function testImportFirefoxWithRabbitEnabled()
23 {
24 $this->logInAs('admin');
25 $client = $this->getClient();
26
27 $client->getContainer()->get('craue_config')->set('import_with_rabbitmq', 1);
28
29 $crawler = $client->request('GET', '/import/firefox');
30
31 $this->assertEquals(200, $client->getResponse()->getStatusCode());
32 $this->assertEquals(1, $crawler->filter('form[name=upload_import_file] > button[type=submit]')->count());
33 $this->assertEquals(1, $crawler->filter('input[type=file]')->count());
34
35 $client->getContainer()->get('craue_config')->set('import_with_rabbitmq', 0);
36 }
37
38 public function testImportFirefoxBadFile()
39 {
40 $this->logInAs('admin');
41 $client = $this->getClient();
42
43 $crawler = $client->request('GET', '/import/firefox');
44 $form = $crawler->filter('form[name=upload_import_file] > button[type=submit]')->form();
45
46 $data = [
47 'upload_import_file[file]' => '',
48 ];
49
50 $client->submit($form, $data);
51
52 $this->assertEquals(200, $client->getResponse()->getStatusCode());
53 }
54
55 public function testImportFirefoxWithRedisEnabled()
56 {
57 $this->logInAs('admin');
58 $client = $this->getClient();
59 $client->getContainer()->get('craue_config')->set('import_with_redis', 1);
60
61 $crawler = $client->request('GET', '/import/firefox');
62
63 $this->assertEquals(200, $client->getResponse()->getStatusCode());
64 $this->assertEquals(1, $crawler->filter('form[name=upload_import_file] > button[type=submit]')->count());
65 $this->assertEquals(1, $crawler->filter('input[type=file]')->count());
66
67 $form = $crawler->filter('form[name=upload_import_file] > button[type=submit]')->form();
68
69 $file = new UploadedFile(__DIR__.'/../fixtures/firefox-bookmarks.json', 'Bookmarks');
70
71 $data = [
72 'upload_import_file[file]' => $file,
73 ];
74
75 $client->submit($form, $data);
76
77 $this->assertEquals(302, $client->getResponse()->getStatusCode());
78
79 $crawler = $client->followRedirect();
80
81 $this->assertGreaterThan(1, $body = $crawler->filter('body')->extract(['_text']));
82 $this->assertContains('flashes.import.notice.summary', $body[0]);
83
84 $this->assertNotEmpty($client->getContainer()->get('wallabag_core.redis.client')->lpop('wallabag.import.firefox'));
85
86 $client->getContainer()->get('craue_config')->set('import_with_redis', 0);
87 }
88
89 public function testImportWallabagWithFirefoxFile()
90 {
91 $this->logInAs('admin');
92 $client = $this->getClient();
93
94 $crawler = $client->request('GET', '/import/firefox');
95 $form = $crawler->filter('form[name=upload_import_file] > button[type=submit]')->form();
96
97 $file = new UploadedFile(__DIR__.'/../fixtures/firefox-bookmarks.json', 'Bookmarks');
98
99 $data = [
100 'upload_import_file[file]' => $file,
101 ];
102
103 $client->submit($form, $data);
104
105 $this->assertEquals(302, $client->getResponse()->getStatusCode());
106
107 $crawler = $client->followRedirect();
108
109 $this->assertGreaterThan(1, $body = $crawler->filter('body')->extract(['_text']));
110 $this->assertContains('flashes.import.notice.summary', $body[0]);
111
112 $content = $client->getContainer()
113 ->get('doctrine.orm.entity_manager')
114 ->getRepository('WallabagCoreBundle:Entry')
115 ->findByUrlAndUserId(
116 'http://lexpansion.lexpress.fr/high-tech/orange-offre-un-meilleur-reseau-mobile-que-bouygues-et-sfr-free-derriere_1811554.html',
117 $this->getLoggedInUserId()
118 );
119
120 $this->assertNotEmpty($content->getMimetype());
121 $this->assertNotEmpty($content->getPreviewPicture());
122 $this->assertNotEmpty($content->getLanguage());
123 $this->assertEquals(2, count($content->getTags()));
124
125 $content = $client->getContainer()
126 ->get('doctrine.orm.entity_manager')
127 ->getRepository('WallabagCoreBundle:Entry')
128 ->findByUrlAndUserId(
129 'http://stackoverflow.com/questions/15017163/parser-for-exported-bookmarks-html-file-of-google-chrome-and-mozilla-in-java',
130 $this->getLoggedInUserId()
131 );
132
133 $this->assertNotEmpty($content->getMimetype());
134 $this->assertNotEmpty($content->getPreviewPicture());
135 $this->assertEmpty($content->getLanguage());
136
137 $createdAt = $content->getCreatedAt();
138 $this->assertEquals('2013', $createdAt->format('Y'));
139 $this->assertEquals('12', $createdAt->format('m'));
140 }
141
142 public function testImportWallabagWithEmptyFile()
143 {
144 $this->logInAs('admin');
145 $client = $this->getClient();
146
147 $crawler = $client->request('GET', '/import/firefox');
148 $form = $crawler->filter('form[name=upload_import_file] > button[type=submit]')->form();
149
150 $file = new UploadedFile(__DIR__.'/../fixtures/test.txt', 'test.txt');
151
152 $data = [
153 'upload_import_file[file]' => $file,
154 ];
155
156 $client->submit($form, $data);
157
158 $this->assertEquals(302, $client->getResponse()->getStatusCode());
159
160 $crawler = $client->followRedirect();
161
162 $this->assertGreaterThan(1, $body = $crawler->filter('body')->extract(['_text']));
163 $this->assertContains('flashes.import.notice.failed', $body[0]);
164 }
165}
diff --git a/tests/Wallabag/ImportBundle/Controller/ImportControllerTest.php b/tests/Wallabag/ImportBundle/Controller/ImportControllerTest.php
index d869cdf9..b6783a56 100644
--- a/tests/Wallabag/ImportBundle/Controller/ImportControllerTest.php
+++ b/tests/Wallabag/ImportBundle/Controller/ImportControllerTest.php
@@ -24,6 +24,6 @@ class ImportControllerTest extends WallabagCoreTestCase
24 $crawler = $client->request('GET', '/import/'); 24 $crawler = $client->request('GET', '/import/');
25 25
26 $this->assertEquals(200, $client->getResponse()->getStatusCode()); 26 $this->assertEquals(200, $client->getResponse()->getStatusCode());
27 $this->assertEquals(4, $crawler->filter('blockquote')->count()); 27 $this->assertEquals(6, $crawler->filter('blockquote')->count());
28 } 28 }
29} 29}
diff --git a/tests/Wallabag/ImportBundle/Controller/ReadabilityControllerTest.php b/tests/Wallabag/ImportBundle/Controller/ReadabilityControllerTest.php
index 87ecb9d3..916dd297 100644
--- a/tests/Wallabag/ImportBundle/Controller/ReadabilityControllerTest.php
+++ b/tests/Wallabag/ImportBundle/Controller/ReadabilityControllerTest.php
@@ -57,7 +57,6 @@ class ReadabilityControllerTest extends WallabagCoreTestCase
57 $this->checkRedis(); 57 $this->checkRedis();
58 $this->logInAs('admin'); 58 $this->logInAs('admin');
59 $client = $this->getClient(); 59 $client = $this->getClient();
60
61 $client->getContainer()->get('craue_config')->set('import_with_redis', 1); 60 $client->getContainer()->get('craue_config')->set('import_with_redis', 1);
62 61
63 $crawler = $client->request('GET', '/import/readability'); 62 $crawler = $client->request('GET', '/import/readability');
diff --git a/tests/Wallabag/ImportBundle/Import/ChromeImportTest.php b/tests/Wallabag/ImportBundle/Import/ChromeImportTest.php
new file mode 100644
index 00000000..1e52615c
--- /dev/null
+++ b/tests/Wallabag/ImportBundle/Import/ChromeImportTest.php
@@ -0,0 +1,233 @@
1<?php
2
3namespace Tests\Wallabag\ImportBundle\Import;
4
5use Wallabag\ImportBundle\Import\ChromeImport;
6use Wallabag\UserBundle\Entity\User;
7use Wallabag\CoreBundle\Entity\Entry;
8use Wallabag\ImportBundle\Redis\Producer;
9use Monolog\Logger;
10use Monolog\Handler\TestHandler;
11use Simpleue\Queue\RedisQueue;
12use M6Web\Component\RedisMock\RedisMockFactory;
13
14class ChromeImportTest extends \PHPUnit_Framework_TestCase
15{
16 protected $user;
17 protected $em;
18 protected $logHandler;
19 protected $contentProxy;
20
21 private function getChromeImport($unsetUser = false)
22 {
23 $this->user = new User();
24
25 $this->em = $this->getMockBuilder('Doctrine\ORM\EntityManager')
26 ->disableOriginalConstructor()
27 ->getMock();
28
29 $this->contentProxy = $this->getMockBuilder('Wallabag\CoreBundle\Helper\ContentProxy')
30 ->disableOriginalConstructor()
31 ->getMock();
32
33 $wallabag = new ChromeImport($this->em, $this->contentProxy);
34
35 $this->logHandler = new TestHandler();
36 $logger = new Logger('test', [$this->logHandler]);
37 $wallabag->setLogger($logger);
38
39 if (false === $unsetUser) {
40 $wallabag->setUser($this->user);
41 }
42
43 return $wallabag;
44 }
45
46 public function testInit()
47 {
48 $chromeImport = $this->getChromeImport();
49
50 $this->assertEquals('Chrome', $chromeImport->getName());
51 $this->assertNotEmpty($chromeImport->getUrl());
52 $this->assertEquals('import.chrome.description', $chromeImport->getDescription());
53 }
54
55 public function testImport()
56 {
57 $chromeImport = $this->getChromeImport();
58 $chromeImport->setFilepath(__DIR__.'/../fixtures/chrome-bookmarks');
59
60 $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository')
61 ->disableOriginalConstructor()
62 ->getMock();
63
64 $entryRepo->expects($this->exactly(1))
65 ->method('findByUrlAndUserId')
66 ->willReturn(false);
67
68 $this->em
69 ->expects($this->any())
70 ->method('getRepository')
71 ->willReturn($entryRepo);
72
73 $entry = $this->getMockBuilder('Wallabag\CoreBundle\Entity\Entry')
74 ->disableOriginalConstructor()
75 ->getMock();
76
77 $this->contentProxy
78 ->expects($this->exactly(1))
79 ->method('updateEntry')
80 ->willReturn($entry);
81
82 $res = $chromeImport->import();
83
84 $this->assertTrue($res);
85 $this->assertEquals(['skipped' => 0, 'imported' => 1, 'queued' => 0], $chromeImport->getSummary());
86 }
87
88 public function testImportAndMarkAllAsRead()
89 {
90 $chromeImport = $this->getChromeImport();
91 $chromeImport->setFilepath(__DIR__.'/../fixtures/chrome-bookmarks');
92
93 $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository')
94 ->disableOriginalConstructor()
95 ->getMock();
96
97 $entryRepo->expects($this->exactly(1))
98 ->method('findByUrlAndUserId')
99 ->will($this->onConsecutiveCalls(false, true));
100
101 $this->em
102 ->expects($this->any())
103 ->method('getRepository')
104 ->willReturn($entryRepo);
105
106 $this->contentProxy
107 ->expects($this->exactly(1))
108 ->method('updateEntry')
109 ->willReturn(new Entry($this->user));
110
111 // check that every entry persisted are archived
112 $this->em
113 ->expects($this->any())
114 ->method('persist')
115 ->with($this->callback(function ($persistedEntry) {
116 return $persistedEntry->isArchived();
117 }));
118
119 $res = $chromeImport->setMarkAsRead(true)->import();
120
121 $this->assertTrue($res);
122
123 $this->assertEquals(['skipped' => 0, 'imported' => 1, 'queued' => 0], $chromeImport->getSummary());
124 }
125
126 public function testImportWithRabbit()
127 {
128 $chromeImport = $this->getChromeImport();
129 $chromeImport->setFilepath(__DIR__.'/../fixtures/chrome-bookmarks');
130
131 $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository')
132 ->disableOriginalConstructor()
133 ->getMock();
134
135 $entryRepo->expects($this->never())
136 ->method('findByUrlAndUserId');
137
138 $this->em
139 ->expects($this->never())
140 ->method('getRepository');
141
142 $entry = $this->getMockBuilder('Wallabag\CoreBundle\Entity\Entry')
143 ->disableOriginalConstructor()
144 ->getMock();
145
146 $this->contentProxy
147 ->expects($this->never())
148 ->method('updateEntry');
149
150 $producer = $this->getMockBuilder('OldSound\RabbitMqBundle\RabbitMq\Producer')
151 ->disableOriginalConstructor()
152 ->getMock();
153
154 $producer
155 ->expects($this->exactly(1))
156 ->method('publish');
157
158 $chromeImport->setProducer($producer);
159
160 $res = $chromeImport->setMarkAsRead(true)->import();
161
162 $this->assertTrue($res);
163 $this->assertEquals(['skipped' => 0, 'imported' => 0, 'queued' => 1], $chromeImport->getSummary());
164 }
165
166 public function testImportWithRedis()
167 {
168 $chromeImport = $this->getChromeImport();
169 $chromeImport->setFilepath(__DIR__.'/../fixtures/chrome-bookmarks');
170
171 $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository')
172 ->disableOriginalConstructor()
173 ->getMock();
174
175 $entryRepo->expects($this->never())
176 ->method('findByUrlAndUserId');
177
178 $this->em
179 ->expects($this->never())
180 ->method('getRepository');
181
182 $entry = $this->getMockBuilder('Wallabag\CoreBundle\Entity\Entry')
183 ->disableOriginalConstructor()
184 ->getMock();
185
186 $this->contentProxy
187 ->expects($this->never())
188 ->method('updateEntry');
189
190 $factory = new RedisMockFactory();
191 $redisMock = $factory->getAdapter('Predis\Client', true);
192
193 $queue = new RedisQueue($redisMock, 'chrome');
194 $producer = new Producer($queue);
195
196 $chromeImport->setProducer($producer);
197
198 $res = $chromeImport->setMarkAsRead(true)->import();
199
200 $this->assertTrue($res);
201 $this->assertEquals(['skipped' => 0, 'imported' => 0, 'queued' => 1], $chromeImport->getSummary());
202
203 $this->assertNotEmpty($redisMock->lpop('chrome'));
204 }
205
206 public function testImportBadFile()
207 {
208 $chromeImport = $this->getChromeImport();
209 $chromeImport->setFilepath(__DIR__.'/../fixtures/wallabag-v1.jsonx');
210
211 $res = $chromeImport->import();
212
213 $this->assertFalse($res);
214
215 $records = $this->logHandler->getRecords();
216 $this->assertContains('Wallabag Browser Import: unable to read file', $records[0]['message']);
217 $this->assertEquals('ERROR', $records[0]['level_name']);
218 }
219
220 public function testImportUserNotDefined()
221 {
222 $chromeImport = $this->getChromeImport(true);
223 $chromeImport->setFilepath(__DIR__.'/../fixtures/chrome-bookmarks');
224
225 $res = $chromeImport->import();
226
227 $this->assertFalse($res);
228
229 $records = $this->logHandler->getRecords();
230 $this->assertContains('Wallabag Browser Import: user is not defined', $records[0]['message']);
231 $this->assertEquals('ERROR', $records[0]['level_name']);
232 }
233}
diff --git a/tests/Wallabag/ImportBundle/Import/FirefoxImportTest.php b/tests/Wallabag/ImportBundle/Import/FirefoxImportTest.php
new file mode 100644
index 00000000..007dda6a
--- /dev/null
+++ b/tests/Wallabag/ImportBundle/Import/FirefoxImportTest.php
@@ -0,0 +1,233 @@
1<?php
2
3namespace Tests\Wallabag\ImportBundle\Import;
4
5use Wallabag\ImportBundle\Import\FirefoxImport;
6use Wallabag\UserBundle\Entity\User;
7use Wallabag\CoreBundle\Entity\Entry;
8use Wallabag\ImportBundle\Redis\Producer;
9use Monolog\Logger;
10use Monolog\Handler\TestHandler;
11use Simpleue\Queue\RedisQueue;
12use M6Web\Component\RedisMock\RedisMockFactory;
13
14class FirefoxImportTest extends \PHPUnit_Framework_TestCase
15{
16 protected $user;
17 protected $em;
18 protected $logHandler;
19 protected $contentProxy;
20
21 private function getFirefoxImport($unsetUser = false)
22 {
23 $this->user = new User();
24
25 $this->em = $this->getMockBuilder('Doctrine\ORM\EntityManager')
26 ->disableOriginalConstructor()
27 ->getMock();
28
29 $this->contentProxy = $this->getMockBuilder('Wallabag\CoreBundle\Helper\ContentProxy')
30 ->disableOriginalConstructor()
31 ->getMock();
32
33 $wallabag = new FirefoxImport($this->em, $this->contentProxy);
34
35 $this->logHandler = new TestHandler();
36 $logger = new Logger('test', [$this->logHandler]);
37 $wallabag->setLogger($logger);
38
39 if (false === $unsetUser) {
40 $wallabag->setUser($this->user);
41 }
42
43 return $wallabag;
44 }
45
46 public function testInit()
47 {
48 $firefoxImport = $this->getFirefoxImport();
49
50 $this->assertEquals('Firefox', $firefoxImport->getName());
51 $this->assertNotEmpty($firefoxImport->getUrl());
52 $this->assertEquals('import.firefox.description', $firefoxImport->getDescription());
53 }
54
55 public function testImport()
56 {
57 $firefoxImport = $this->getFirefoxImport();
58 $firefoxImport->setFilepath(__DIR__.'/../fixtures/firefox-bookmarks.json');
59
60 $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository')
61 ->disableOriginalConstructor()
62 ->getMock();
63
64 $entryRepo->expects($this->exactly(2))
65 ->method('findByUrlAndUserId')
66 ->willReturn(false);
67
68 $this->em
69 ->expects($this->any())
70 ->method('getRepository')
71 ->willReturn($entryRepo);
72
73 $entry = $this->getMockBuilder('Wallabag\CoreBundle\Entity\Entry')
74 ->disableOriginalConstructor()
75 ->getMock();
76
77 $this->contentProxy
78 ->expects($this->exactly(2))
79 ->method('updateEntry')
80 ->willReturn($entry);
81
82 $res = $firefoxImport->import();
83
84 $this->assertTrue($res);
85 $this->assertEquals(['skipped' => 0, 'imported' => 2, 'queued' => 0], $firefoxImport->getSummary());
86 }
87
88 public function testImportAndMarkAllAsRead()
89 {
90 $firefoxImport = $this->getFirefoxImport();
91 $firefoxImport->setFilepath(__DIR__.'/../fixtures/firefox-bookmarks.json');
92
93 $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository')
94 ->disableOriginalConstructor()
95 ->getMock();
96
97 $entryRepo->expects($this->exactly(2))
98 ->method('findByUrlAndUserId')
99 ->will($this->onConsecutiveCalls(false, true));
100
101 $this->em
102 ->expects($this->any())
103 ->method('getRepository')
104 ->willReturn($entryRepo);
105
106 $this->contentProxy
107 ->expects($this->exactly(1))
108 ->method('updateEntry')
109 ->willReturn(new Entry($this->user));
110
111 // check that every entry persisted are archived
112 $this->em
113 ->expects($this->any())
114 ->method('persist')
115 ->with($this->callback(function ($persistedEntry) {
116 return $persistedEntry->isArchived();
117 }));
118
119 $res = $firefoxImport->setMarkAsRead(true)->import();
120
121 $this->assertTrue($res);
122
123 $this->assertEquals(['skipped' => 1, 'imported' => 1, 'queued' => 0], $firefoxImport->getSummary());
124 }
125
126 public function testImportWithRabbit()
127 {
128 $firefoxImport = $this->getFirefoxImport();
129 $firefoxImport->setFilepath(__DIR__.'/../fixtures/firefox-bookmarks.json');
130
131 $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository')
132 ->disableOriginalConstructor()
133 ->getMock();
134
135 $entryRepo->expects($this->never())
136 ->method('findByUrlAndUserId');
137
138 $this->em
139 ->expects($this->never())
140 ->method('getRepository');
141
142 $entry = $this->getMockBuilder('Wallabag\CoreBundle\Entity\Entry')
143 ->disableOriginalConstructor()
144 ->getMock();
145
146 $this->contentProxy
147 ->expects($this->never())
148 ->method('updateEntry');
149
150 $producer = $this->getMockBuilder('OldSound\RabbitMqBundle\RabbitMq\Producer')
151 ->disableOriginalConstructor()
152 ->getMock();
153
154 $producer
155 ->expects($this->exactly(1))
156 ->method('publish');
157
158 $firefoxImport->setProducer($producer);
159
160 $res = $firefoxImport->setMarkAsRead(true)->import();
161
162 $this->assertTrue($res);
163 $this->assertEquals(['skipped' => 0, 'imported' => 0, 'queued' => 1], $firefoxImport->getSummary());
164 }
165
166 public function testImportWithRedis()
167 {
168 $firefoxImport = $this->getFirefoxImport();
169 $firefoxImport->setFilepath(__DIR__.'/../fixtures/firefox-bookmarks.json');
170
171 $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository')
172 ->disableOriginalConstructor()
173 ->getMock();
174
175 $entryRepo->expects($this->never())
176 ->method('findByUrlAndUserId');
177
178 $this->em
179 ->expects($this->never())
180 ->method('getRepository');
181
182 $entry = $this->getMockBuilder('Wallabag\CoreBundle\Entity\Entry')
183 ->disableOriginalConstructor()
184 ->getMock();
185
186 $this->contentProxy
187 ->expects($this->never())
188 ->method('updateEntry');
189
190 $factory = new RedisMockFactory();
191 $redisMock = $factory->getAdapter('Predis\Client', true);
192
193 $queue = new RedisQueue($redisMock, 'firefox');
194 $producer = new Producer($queue);
195
196 $firefoxImport->setProducer($producer);
197
198 $res = $firefoxImport->setMarkAsRead(true)->import();
199
200 $this->assertTrue($res);
201 $this->assertEquals(['skipped' => 0, 'imported' => 0, 'queued' => 1], $firefoxImport->getSummary());
202
203 $this->assertNotEmpty($redisMock->lpop('firefox'));
204 }
205
206 public function testImportBadFile()
207 {
208 $firefoxImport = $this->getFirefoxImport();
209 $firefoxImport->setFilepath(__DIR__.'/../fixtures/wallabag-v1.jsonx');
210
211 $res = $firefoxImport->import();
212
213 $this->assertFalse($res);
214
215 $records = $this->logHandler->getRecords();
216 $this->assertContains('Wallabag Browser Import: unable to read file', $records[0]['message']);
217 $this->assertEquals('ERROR', $records[0]['level_name']);
218 }
219
220 public function testImportUserNotDefined()
221 {
222 $firefoxImport = $this->getFirefoxImport(true);
223 $firefoxImport->setFilepath(__DIR__.'/../fixtures/firefox-bookmarks.json');
224
225 $res = $firefoxImport->import();
226
227 $this->assertFalse($res);
228
229 $records = $this->logHandler->getRecords();
230 $this->assertContains('Wallabag Browser Import: user is not defined', $records[0]['message']);
231 $this->assertEquals('ERROR', $records[0]['level_name']);
232 }
233}
diff --git a/tests/Wallabag/ImportBundle/fixtures/chrome-bookmarks b/tests/Wallabag/ImportBundle/fixtures/chrome-bookmarks
new file mode 100644
index 00000000..0478eb41
--- /dev/null
+++ b/tests/Wallabag/ImportBundle/fixtures/chrome-bookmarks
@@ -0,0 +1,36 @@
1{
2 "checksum": "f3aa0e9c0edad632a246f7e98ec64918",
3 "roots": {
4 "bookmark_bar": {
5 "children": [ {
6 "date_added": "13118850929335823",
7 "id": "6",
8 "name": "\"La multiplication des chefs de projet est une catastrophe managériale majeure\", affirme le sociologue François Dupuy - Ressources humaines",
9 "type": "url",
10 "url": "http://www.usinenouvelle.com/article/la-multiplication-des-chefs-de-projet-est-une-catastrophe-manageriale-majeure-affirme-le-sociologue-francois-dupuy.N307730"
11 } ],
12 "date_added": "13118829474385693",
13 "date_modified": "13118850929335823",
14 "id": "1",
15 "name": "Barre de favoris",
16 "type": "folder"
17 },
18 "other": {
19 "children": [ ],
20 "date_added": "13118829474385701",
21 "date_modified": "0",
22 "id": "2",
23 "name": "Autres favoris",
24 "type": "folder"
25 },
26 "synced": {
27 "children": [ ],
28 "date_added": "13118829474385702",
29 "date_modified": "0",
30 "id": "3",
31 "name": "Favoris sur mobile",
32 "type": "folder"
33 }
34 },
35 "version": 1
36}
diff --git a/tests/Wallabag/ImportBundle/fixtures/firefox-bookmarks.json b/tests/Wallabag/ImportBundle/fixtures/firefox-bookmarks.json
new file mode 100644
index 00000000..406b5697
--- /dev/null
+++ b/tests/Wallabag/ImportBundle/fixtures/firefox-bookmarks.json
@@ -0,0 +1,63 @@
1{
2 "guid": "root________",
3 "title": "",
4 "index": 0,
5 "dateAdded": 1388166091504000,
6 "lastModified": 1472897622350000,
7 "id": 1,
8 "type": "text/x-moz-place-container",
9 "root": "placesRoot",
10 "children": [
11 {
12 "guid": "toolbar_____",
13 "title": "Barre personnelle",
14 "index": 1,
15 "dateAdded": 1388166091504000,
16 "lastModified": 1472897622263000,
17 "id": 3,
18 "annos": [
19 {
20 "name": "bookmarkProperties/description",
21 "flags": 0,
22 "expires": 4,
23 "value": "Ajoutez des marque-pages dans ce dossier pour les voir apparaître sur votre barre personnelle"
24 }
25 ],
26 "type": "text/x-moz-place-container",
27 "root": "toolbarFolder",
28 "children": [
29 {
30 "guid": "tard77lzbC5H",
31 "title": "Orange offre un meilleur réseau mobile que Bouygues et SFR, Free derrière - L'Express L'Expansion",
32 "index": 1,
33 "dateAdded": 1388166091644000,
34 "lastModified": 1388166091644000,
35 "tags":"test,tag",
36 "id": 4,
37 "type": "text/x-moz-place",
38 "uri": "http://lexpansion.lexpress.fr/high-tech/orange-offre-un-meilleur-reseau-mobile-que-bouygues-et-sfr-free-derriere_1811554.html"
39 },
40 {
41 "guid": "E385l9vZ_LVn",
42 "title": "Parser for Exported Bookmarks HTML file of Google Chrome and Mozilla in Java",
43 "index": 1,
44 "dateAdded": 1388166091544000,
45 "lastModified": 1388166091545000,
46 "id": 5,
47 "type": "text/x-moz-place",
48 "uri": "http://stackoverflow.com/questions/15017163/parser-for-exported-bookmarks-html-file-of-google-chrome-and-mozilla-in-java"
49 }
50 ]
51 },
52 {
53 "guid": "unfiled_____",
54 "title": "Autres marque-pages",
55 "index": 3,
56 "dateAdded": 1388166091504000,
57 "lastModified": 1388166091542000,
58 "id": 6,
59 "type": "text/x-moz-place-container",
60 "root": "unfiledBookmarksFolder"
61 }
62 ]
63}