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