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