]> git.immae.eu Git - github/wallabag/wallabag.git/blame - src/Wallabag/ImportBundle/Tests/Import/WallabagV1ImportTest.php
Convert english translation file
[github/wallabag/wallabag.git] / src / Wallabag / ImportBundle / Tests / Import / WallabagV1ImportTest.php
CommitLineData
b1d05721
JB
1<?php
2
3namespace Wallabag\ImportBundle\Tests\Import;
4
5use Wallabag\UserBundle\Entity\User;
6use Wallabag\ImportBundle\Import\WallabagV1Import;
7use Monolog\Logger;
8use Monolog\Handler\TestHandler;
9
10class WallabagV1ImportTest extends \PHPUnit_Framework_TestCase
11{
12 protected $user;
13 protected $em;
14 protected $logHandler;
0783c99a
TC
15 protected $contentProxy;
16
b1d05721
JB
17 private function getWallabagV1Import($unsetUser = false)
18 {
19 $this->user = new User();
20
21 $this->em = $this->getMockBuilder('Doctrine\ORM\EntityManager')
22 ->disableOriginalConstructor()
23 ->getMock();
24
0783c99a
TC
25 $this->contentProxy = $this->getMockBuilder('Wallabag\CoreBundle\Helper\ContentProxy')
26 ->disableOriginalConstructor()
27 ->getMock();
28
da0a9e01 29 $wallabag = new WallabagV1Import($this->em, $this->contentProxy);
b1d05721
JB
30
31 $this->logHandler = new TestHandler();
32 $logger = new Logger('test', array($this->logHandler));
6785f4aa 33 $wallabag->setLogger($logger);
b1d05721
JB
34
35 if (false === $unsetUser) {
6785f4aa 36 $wallabag->setUser($this->user);
b1d05721
JB
37 }
38
6785f4aa 39 return $wallabag;
b1d05721
JB
40 }
41
42 public function testInit()
43 {
44 $wallabagV1Import = $this->getWallabagV1Import();
45
b88cf91f 46 $this->assertEquals('wallabag v1', $wallabagV1Import->getName());
7019c7cf 47 $this->assertNotEmpty($wallabagV1Import->getUrl());
0d42217e 48 $this->assertEquals('import.wallabag_v1.description', $wallabagV1Import->getDescription());
b1d05721
JB
49 }
50
51 public function testImport()
52 {
53 $wallabagV1Import = $this->getWallabagV1Import();
54 $wallabagV1Import->setFilepath(__DIR__.'/../fixtures/wallabag-v1.json');
55
56 $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository')
57 ->disableOriginalConstructor()
58 ->getMock();
59
eaf9dad7 60 $entryRepo->expects($this->exactly(4))
78833672 61 ->method('findByUrlAndUserId')
eaf9dad7 62 ->will($this->onConsecutiveCalls(false, true, false, false));
b1d05721
JB
63
64 $this->em
65 ->expects($this->any())
66 ->method('getRepository')
67 ->willReturn($entryRepo);
68
eaf9dad7
TC
69 $entry = $this->getMockBuilder('Wallabag\CoreBundle\Entity\Entry')
70 ->disableOriginalConstructor()
71 ->getMock();
72
73 $this->contentProxy
74 ->expects($this->once())
75 ->method('updateEntry')
76 ->willReturn($entry);
77
b1d05721
JB
78 $res = $wallabagV1Import->import();
79
80 $this->assertTrue($res);
eaf9dad7 81 $this->assertEquals(['skipped' => 1, 'imported' => 3], $wallabagV1Import->getSummary());
79d0e38e
JB
82 }
83
84 public function testImportAndMarkAllAsRead()
85 {
86 $wallabagV1Import = $this->getWallabagV1Import();
87 $wallabagV1Import->setFilepath(__DIR__.'/../fixtures/wallabag-v1-read.json');
88
89 $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository')
90 ->disableOriginalConstructor()
91 ->getMock();
92
93 $entryRepo->expects($this->exactly(3))
94 ->method('findByUrlAndUserId')
95 ->will($this->onConsecutiveCalls(false, false, false));
96
97 $this->em
98 ->expects($this->any())
99 ->method('getRepository')
100 ->willReturn($entryRepo);
101
102 // check that every entry persisted are archived
103 $this->em
104 ->expects($this->any())
105 ->method('persist')
cebb4223 106 ->with($this->callback(function ($persistedEntry) {
79d0e38e
JB
107 return $persistedEntry->isArchived();
108 }));
109
110 $res = $wallabagV1Import->setMarkAsRead(true)->import();
111
112 $this->assertTrue($res);
113
114 $this->assertEquals(['skipped' => 0, 'imported' => 3], $wallabagV1Import->getSummary());
b1d05721
JB
115 }
116
117 public function testImportBadFile()
118 {
119 $wallabagV1Import = $this->getWallabagV1Import();
120 $wallabagV1Import->setFilepath(__DIR__.'/../fixtures/wallabag-v1.jsonx');
121
122 $res = $wallabagV1Import->import();
123
124 $this->assertFalse($res);
125
126 $records = $this->logHandler->getRecords();
6785f4aa 127 $this->assertContains('WallabagImport: unable to read file', $records[0]['message']);
b1d05721
JB
128 $this->assertEquals('ERROR', $records[0]['level_name']);
129 }
130
131 public function testImportUserNotDefined()
132 {
133 $wallabagV1Import = $this->getWallabagV1Import(true);
134 $wallabagV1Import->setFilepath(__DIR__.'/../fixtures/wallabag-v1.json');
135
136 $res = $wallabagV1Import->import();
137
138 $this->assertFalse($res);
139
140 $records = $this->logHandler->getRecords();
6785f4aa 141 $this->assertContains('WallabagImport: user is not defined', $records[0]['message']);
b1d05721
JB
142 $this->assertEquals('ERROR', $records[0]['level_name']);
143 }
144}