aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests/Wallabag/ImportBundle
diff options
context:
space:
mode:
Diffstat (limited to 'tests/Wallabag/ImportBundle')
-rw-r--r--tests/Wallabag/ImportBundle/Controller/ImportControllerTest.php2
-rw-r--r--tests/Wallabag/ImportBundle/Controller/ReadabilityControllerTest.php122
-rw-r--r--tests/Wallabag/ImportBundle/Import/ReadabilityImportTest.php150
-rw-r--r--tests/Wallabag/ImportBundle/fixtures/readability-read.json25
-rw-r--r--tests/Wallabag/ImportBundle/fixtures/readability.json25
5 files changed, 323 insertions, 1 deletions
diff --git a/tests/Wallabag/ImportBundle/Controller/ImportControllerTest.php b/tests/Wallabag/ImportBundle/Controller/ImportControllerTest.php
index 96b5300b..d869cdf9 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(3, $crawler->filter('blockquote')->count()); 27 $this->assertEquals(4, $crawler->filter('blockquote')->count());
28 } 28 }
29} 29}
diff --git a/tests/Wallabag/ImportBundle/Controller/ReadabilityControllerTest.php b/tests/Wallabag/ImportBundle/Controller/ReadabilityControllerTest.php
new file mode 100644
index 00000000..92cf4bfc
--- /dev/null
+++ b/tests/Wallabag/ImportBundle/Controller/ReadabilityControllerTest.php
@@ -0,0 +1,122 @@
1<?php
2
3namespace Tests\Wallabag\ImportBundle\Controller;
4
5use Tests\Wallabag\CoreBundle\WallabagCoreTestCase;
6use Symfony\Component\HttpFoundation\File\UploadedFile;
7
8class ReadabilityControllerTest extends WallabagCoreTestCase
9{
10 public function testImportReadability()
11 {
12 $this->logInAs('admin');
13 $client = $this->getClient();
14
15 $crawler = $client->request('GET', '/import/readability');
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 testImportReadabilityWithFile()
23 {
24 $this->logInAs('admin');
25 $client = $this->getClient();
26
27 $crawler = $client->request('GET', '/import/readability');
28 $form = $crawler->filter('form[name=upload_import_file] > button[type=submit]')->form();
29
30 $file = new UploadedFile(__DIR__.'/../fixtures/readability.json', 'readability.json');
31
32 $data = [
33 'upload_import_file[file]' => $file,
34 ];
35
36 $client->submit($form, $data);
37
38 $this->assertEquals(302, $client->getResponse()->getStatusCode());
39
40 $crawler = $client->followRedirect();
41
42 $content = $client->getContainer()
43 ->get('doctrine.orm.entity_manager')
44 ->getRepository('WallabagCoreBundle:Entry')
45 ->findByUrlAndUserId(
46 'https://venngage.com/blog/hashtags-are-worthless/',
47 $this->getLoggedInUserId()
48 );
49
50 $this->assertGreaterThan(1, $body = $crawler->filter('body')->extract(['_text']));
51 $this->assertContains('flashes.import.notice.summary', $body[0]);
52 }
53
54 public function testImportReadabilityWithFileAndMarkAllAsRead()
55 {
56 $this->logInAs('admin');
57 $client = $this->getClient();
58
59 $crawler = $client->request('GET', '/import/readability');
60 $form = $crawler->filter('form[name=upload_import_file] > button[type=submit]')->form();
61
62 $file = new UploadedFile(__DIR__.'/../fixtures/readability-read.json', 'readability-read.json');
63
64 $data = [
65 'upload_import_file[file]' => $file,
66 'upload_import_file[mark_as_read]' => 1,
67 ];
68
69 $client->submit($form, $data);
70
71 $this->assertEquals(302, $client->getResponse()->getStatusCode());
72
73 $crawler = $client->followRedirect();
74
75 $content1 = $client->getContainer()
76 ->get('doctrine.orm.entity_manager')
77 ->getRepository('WallabagCoreBundle:Entry')
78 ->findByUrlAndUserId(
79 'https://blog.travis-ci.com/2016-07-28-what-we-learned-from-analyzing-2-million-travis-builds/',
80 $this->getLoggedInUserId()
81 );
82
83 $this->assertTrue($content1->isArchived());
84
85 $content2 = $client->getContainer()
86 ->get('doctrine.orm.entity_manager')
87 ->getRepository('WallabagCoreBundle:Entry')
88 ->findByUrlAndUserId(
89 'https://facebook.github.io/graphql/',
90 $this->getLoggedInUserId()
91 );
92
93 $this->assertTrue($content2->isArchived());
94
95 $this->assertGreaterThan(1, $body = $crawler->filter('body')->extract(['_text']));
96 $this->assertContains('flashes.import.notice.summary', $body[0]);
97 }
98
99 public function testImportReadabilityWithEmptyFile()
100 {
101 $this->logInAs('admin');
102 $client = $this->getClient();
103
104 $crawler = $client->request('GET', '/import/readability');
105 $form = $crawler->filter('form[name=upload_import_file] > button[type=submit]')->form();
106
107 $file = new UploadedFile(__DIR__.'/../fixtures/test.txt', 'test.txt');
108
109 $data = [
110 'upload_import_file[file]' => $file,
111 ];
112
113 $client->submit($form, $data);
114
115 $this->assertEquals(302, $client->getResponse()->getStatusCode());
116
117 $crawler = $client->followRedirect();
118
119 $this->assertGreaterThan(1, $body = $crawler->filter('body')->extract(['_text']));
120 $this->assertContains('flashes.import.notice.failed', $body[0]);
121 }
122}
diff --git a/tests/Wallabag/ImportBundle/Import/ReadabilityImportTest.php b/tests/Wallabag/ImportBundle/Import/ReadabilityImportTest.php
new file mode 100644
index 00000000..706d707b
--- /dev/null
+++ b/tests/Wallabag/ImportBundle/Import/ReadabilityImportTest.php
@@ -0,0 +1,150 @@
1<?php
2
3namespace Tests\Wallabag\ImportBundle\Import;
4
5use Wallabag\ImportBundle\Import\ReadabilityImport;
6use Wallabag\UserBundle\Entity\User;
7use Wallabag\CoreBundle\Entity\Entry;
8use Monolog\Logger;
9use Monolog\Handler\TestHandler;
10
11class ReadabilityImportTest extends \PHPUnit_Framework_TestCase
12{
13 protected $user;
14 protected $em;
15 protected $logHandler;
16 protected $contentProxy;
17
18 private function getReadabilityImport($unsetUser = false)
19 {
20 $this->user = new User();
21
22 $this->em = $this->getMockBuilder('Doctrine\ORM\EntityManager')
23 ->disableOriginalConstructor()
24 ->getMock();
25
26 $this->contentProxy = $this->getMockBuilder('Wallabag\CoreBundle\Helper\ContentProxy')
27 ->disableOriginalConstructor()
28 ->getMock();
29
30 $wallabag = new ReadabilityImport($this->em, $this->contentProxy);
31
32 $this->logHandler = new TestHandler();
33 $logger = new Logger('test', [$this->logHandler]);
34 $wallabag->setLogger($logger);
35
36 if (false === $unsetUser) {
37 $wallabag->setUser($this->user);
38 }
39
40 return $wallabag;
41 }
42
43 public function testInit()
44 {
45 $readabilityImport = $this->getReadabilityImport();
46
47 $this->assertEquals('Readability', $readabilityImport->getName());
48 $this->assertNotEmpty($readabilityImport->getUrl());
49 $this->assertEquals('import.readability.description', $readabilityImport->getDescription());
50 }
51
52 public function testImport()
53 {
54 $readabilityImport = $this->getReadabilityImport();
55 $readabilityImport->setFilepath(__DIR__.'/../fixtures/readability.json');
56
57 $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository')
58 ->disableOriginalConstructor()
59 ->getMock();
60
61 $entryRepo->expects($this->exactly(2))
62 ->method('findByUrlAndUserId')
63 ->will($this->onConsecutiveCalls(false, true));
64
65 $this->em
66 ->expects($this->any())
67 ->method('getRepository')
68 ->willReturn($entryRepo);
69
70 $entry = $this->getMockBuilder('Wallabag\CoreBundle\Entity\Entry')
71 ->disableOriginalConstructor()
72 ->getMock();
73
74 $this->contentProxy
75 ->expects($this->exactly(1))
76 ->method('updateEntry')
77 ->willReturn($entry);
78
79 $res = $readabilityImport->import();
80
81 $this->assertTrue($res);
82 $this->assertEquals(['skipped' => 1, 'imported' => 1], $readabilityImport->getSummary());
83 }
84
85 public function testImportAndMarkAllAsRead()
86 {
87 $readabilityImport = $this->getReadabilityImport();
88 $readabilityImport->setFilepath(__DIR__.'/../fixtures/readability-read.json');
89
90 $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository')
91 ->disableOriginalConstructor()
92 ->getMock();
93
94 $entryRepo->expects($this->exactly(2))
95 ->method('findByUrlAndUserId')
96 ->will($this->onConsecutiveCalls(false, false));
97
98 $this->em
99 ->expects($this->any())
100 ->method('getRepository')
101 ->willReturn($entryRepo);
102
103 $this->contentProxy
104 ->expects($this->exactly(2))
105 ->method('updateEntry')
106 ->willReturn(new Entry($this->user));
107
108 // check that every entry persisted are archived
109 $this->em
110 ->expects($this->any())
111 ->method('persist')
112 ->with($this->callback(function ($persistedEntry) {
113 return $persistedEntry->isArchived();
114 }));
115
116 $res = $readabilityImport->setMarkAsRead(true)->import();
117
118 $this->assertTrue($res);
119
120 $this->assertEquals(['skipped' => 0, 'imported' => 2], $readabilityImport->getSummary());
121 }
122
123 public function testImportBadFile()
124 {
125 $readabilityImport = $this->getReadabilityImport();
126 $readabilityImport->setFilepath(__DIR__.'/../fixtures/wallabag-v1.jsonx');
127
128 $res = $readabilityImport->import();
129
130 $this->assertFalse($res);
131
132 $records = $this->logHandler->getRecords();
133 $this->assertContains('ReadabilityImport: unable to read file', $records[0]['message']);
134 $this->assertEquals('ERROR', $records[0]['level_name']);
135 }
136
137 public function testImportUserNotDefined()
138 {
139 $readabilityImport = $this->getReadabilityImport(true);
140 $readabilityImport->setFilepath(__DIR__.'/../fixtures/readability.json');
141
142 $res = $readabilityImport->import();
143
144 $this->assertFalse($res);
145
146 $records = $this->logHandler->getRecords();
147 $this->assertContains('ReadabilityImport: user is not defined', $records[0]['message']);
148 $this->assertEquals('ERROR', $records[0]['level_name']);
149 }
150}
diff --git a/tests/Wallabag/ImportBundle/fixtures/readability-read.json b/tests/Wallabag/ImportBundle/fixtures/readability-read.json
new file mode 100644
index 00000000..c60767dc
--- /dev/null
+++ b/tests/Wallabag/ImportBundle/fixtures/readability-read.json
@@ -0,0 +1,25 @@
1{
2 "bookmarks": [
3 {
4 "article__excerpt": "This is a guest post from Moritz Beller from the Delft University of Technology in The Netherlands. His team produced amazing research on several million Travis CI builds, creating invaluable&hellip;",
5 "favorite": false,
6 "date_archived": "2016-08-02T06:49:30",
7 "article__url": "https://blog.travis-ci.com/2016-07-28-what-we-learned-from-analyzing-2-million-travis-builds/",
8 "date_added": "2016-08-01T05:24:16",
9 "date_favorited": null,
10 "article__title": "Travis",
11 "archive": true
12 },
13 {
14 "article__excerpt": "The GraphQL Type system describes the capabilities of a GraphQL server and is used to determine if a query is valid. The type system also describes the input types of query variables to determine if&hellip;",
15 "favorite": false,
16 "date_archived": "2016-07-19T06:48:31",
17 "article__url": "https://facebook.github.io/graphql/",
18 "date_added": "2016-06-24T17:50:16",
19 "date_favorited": null,
20 "article__title": "GraphQL",
21 "archive": true
22 }
23 ],
24 "recommendations": []
25}
diff --git a/tests/Wallabag/ImportBundle/fixtures/readability.json b/tests/Wallabag/ImportBundle/fixtures/readability.json
new file mode 100644
index 00000000..34379905
--- /dev/null
+++ b/tests/Wallabag/ImportBundle/fixtures/readability.json
@@ -0,0 +1,25 @@
1{
2 "bookmarks": [
3 {
4 "article__excerpt": "When Twitter started it had so much promise to change the way we communicate. But now it has been ruined by the amount of garbage and hate we have to wade through. It&#x2019;s like that polluted&hellip;",
5 "favorite": false,
6 "date_archived": null,
7 "article__url": "https://venngage.com/blog/hashtags-are-worthless/",
8 "date_added": "2016-08-25T12:05:00",
9 "date_favorited": null,
10 "article__title": "We Looked At 167,943 Tweets & Found Out Hashtags Are Worthless",
11 "archive": false
12 },
13 {
14 "article__excerpt": "TL;DR: Re-use your DOM elements and remove the ones that are far away from the viewport. Use placeholders to account for delayed data. Here&#x2019;s a demo and the code for the infinite&hellip;",
15 "favorite": false,
16 "date_archived": "2016-08-26T12:21:54",
17 "article__url": "https://developers.google.com/web/updates/2016/07/infinite-scroller?imm_mid=0e6839&cmp=em-webops-na-na-newsltr_20160805",
18 "date_added": "2016-08-06T05:35:26",
19 "date_favorited": null,
20 "article__title": "Complexities of an infinite scroller | Web Updates",
21 "archive": true
22 }
23 ],
24 "recommendations": []
25}