aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests/Wallabag/CoreBundle
diff options
context:
space:
mode:
Diffstat (limited to 'tests/Wallabag/CoreBundle')
-rw-r--r--tests/Wallabag/CoreBundle/Command/CleanDuplicatesCommandTest.php108
-rw-r--r--tests/Wallabag/CoreBundle/Command/ExportCommandTest.php6
-rw-r--r--tests/Wallabag/CoreBundle/Command/InstallCommandTest.php185
-rw-r--r--tests/Wallabag/CoreBundle/Command/ShowUserCommandTest.php93
-rw-r--r--tests/Wallabag/CoreBundle/Command/TagAllCommandTest.php2
-rw-r--r--tests/Wallabag/CoreBundle/Controller/ConfigControllerTest.php91
-rw-r--r--tests/Wallabag/CoreBundle/Controller/EntryControllerTest.php582
-rw-r--r--tests/Wallabag/CoreBundle/Controller/ExportControllerTest.php4
-rw-r--r--tests/Wallabag/CoreBundle/Controller/RssControllerTest.php43
-rw-r--r--tests/Wallabag/CoreBundle/Controller/SiteCredentialControllerTest.php139
-rw-r--r--tests/Wallabag/CoreBundle/Controller/TagControllerTest.php59
-rw-r--r--tests/Wallabag/CoreBundle/GuzzleSiteAuthenticator/GrabySiteConfigBuilderTest.php97
-rw-r--r--tests/Wallabag/CoreBundle/Helper/ContentProxyTest.php373
-rw-r--r--tests/Wallabag/CoreBundle/Helper/CryptoProxyTest.php40
-rw-r--r--tests/Wallabag/CoreBundle/Helper/DownloadImagesTest.php49
-rw-r--r--tests/Wallabag/CoreBundle/Helper/RedirectTest.php18
-rw-r--r--tests/Wallabag/CoreBundle/Helper/RuleBasedTaggerTest.php27
-rw-r--r--tests/Wallabag/CoreBundle/Helper/TagsAssignerTest.php108
-rw-r--r--tests/Wallabag/CoreBundle/ParamConverter/UsernameRssTokenConverterTest.php2
-rw-r--r--tests/Wallabag/CoreBundle/WallabagCoreTestCase.php76
-rw-r--r--tests/Wallabag/CoreBundle/fixtures/image-no-content-type.jpgbin0 -> 354067 bytes
21 files changed, 1631 insertions, 471 deletions
diff --git a/tests/Wallabag/CoreBundle/Command/CleanDuplicatesCommandTest.php b/tests/Wallabag/CoreBundle/Command/CleanDuplicatesCommandTest.php
new file mode 100644
index 00000000..e6e57f30
--- /dev/null
+++ b/tests/Wallabag/CoreBundle/Command/CleanDuplicatesCommandTest.php
@@ -0,0 +1,108 @@
1<?php
2
3namespace Tests\Wallabag\CoreBundle\Command;
4
5use Symfony\Bundle\FrameworkBundle\Console\Application;
6use Symfony\Component\Console\Tester\CommandTester;
7use Wallabag\CoreBundle\Command\CleanDuplicatesCommand;
8use Tests\Wallabag\CoreBundle\WallabagCoreTestCase;
9use Wallabag\CoreBundle\Entity\Entry;
10
11class CleanDuplicatesCommandTest extends WallabagCoreTestCase
12{
13 public function testRunCleanDuplicates()
14 {
15 $application = new Application($this->getClient()->getKernel());
16 $application->add(new CleanDuplicatesCommand());
17
18 $command = $application->find('wallabag:clean-duplicates');
19
20 $tester = new CommandTester($command);
21 $tester->execute([
22 'command' => $command->getName(),
23 ]);
24
25 $this->assertContains('Cleaning through 3 user accounts', $tester->getDisplay());
26 $this->assertContains('Finished cleaning. 0 duplicates found in total', $tester->getDisplay());
27 }
28
29 public function testRunCleanDuplicatesCommandWithBadUsername()
30 {
31 $application = new Application($this->getClient()->getKernel());
32 $application->add(new CleanDuplicatesCommand());
33
34 $command = $application->find('wallabag:clean-duplicates');
35
36 $tester = new CommandTester($command);
37 $tester->execute([
38 'command' => $command->getName(),
39 'username' => 'unknown',
40 ]);
41
42 $this->assertContains('User "unknown" not found', $tester->getDisplay());
43 }
44
45 public function testRunCleanDuplicatesCommandForUser()
46 {
47 $application = new Application($this->getClient()->getKernel());
48 $application->add(new CleanDuplicatesCommand());
49
50 $command = $application->find('wallabag:clean-duplicates');
51
52 $tester = new CommandTester($command);
53 $tester->execute([
54 'command' => $command->getName(),
55 'username' => 'admin',
56 ]);
57
58 $this->assertContains('Cleaned 0 duplicates for user admin', $tester->getDisplay());
59 }
60
61 public function testDuplicate()
62 {
63 $url = 'http://www.lemonde.fr/sport/visuel/2017/05/05/rondelle-prison-blanchissage-comprendre-le-hockey-sur-glace_5122587_3242.html';
64 $client = $this->getClient();
65 $em = $client->getContainer()->get('doctrine.orm.entity_manager');
66
67 $this->logInAs('admin');
68
69 $nbEntries = $em->getRepository('WallabagCoreBundle:Entry')->findAllByUrlAndUserId($url, $this->getLoggedInUserId());
70 $this->assertCount(0, $nbEntries);
71
72 $user = $em->getRepository('WallabagUserBundle:User')->findOneById($this->getLoggedInUserId());
73
74 $entry1 = new Entry($user);
75 $entry1->setUrl($url);
76
77 $entry2 = new Entry($user);
78 $entry2->setUrl($url);
79
80 $em->persist($entry1);
81 $em->persist($entry2);
82
83 $em->flush();
84
85 $nbEntries = $em->getRepository('WallabagCoreBundle:Entry')->findAllByUrlAndUserId($url, $this->getLoggedInUserId());
86 $this->assertCount(2, $nbEntries);
87
88 $application = new Application($this->getClient()->getKernel());
89 $application->add(new CleanDuplicatesCommand());
90
91 $command = $application->find('wallabag:clean-duplicates');
92
93 $tester = new CommandTester($command);
94 $tester->execute([
95 'command' => $command->getName(),
96 'username' => 'admin',
97 ]);
98
99 $this->assertContains('Cleaned 1 duplicates for user admin', $tester->getDisplay());
100
101 $nbEntries = $em->getRepository('WallabagCoreBundle:Entry')->findAllByUrlAndUserId($url, $this->getLoggedInUserId());
102 $this->assertCount(1, $nbEntries);
103
104 $query = $em->createQuery('DELETE FROM Wallabag\CoreBundle\Entity\Entry e WHERE e.url = :url');
105 $query->setParameter('url', $url);
106 $query->execute();
107 }
108}
diff --git a/tests/Wallabag/CoreBundle/Command/ExportCommandTest.php b/tests/Wallabag/CoreBundle/Command/ExportCommandTest.php
index 6798c5d7..2eebf39b 100644
--- a/tests/Wallabag/CoreBundle/Command/ExportCommandTest.php
+++ b/tests/Wallabag/CoreBundle/Command/ExportCommandTest.php
@@ -10,7 +10,7 @@ use Tests\Wallabag\CoreBundle\WallabagCoreTestCase;
10class ExportCommandTest extends WallabagCoreTestCase 10class ExportCommandTest extends WallabagCoreTestCase
11{ 11{
12 /** 12 /**
13 * @expectedException Symfony\Component\Console\Exception\RuntimeException 13 * @expectedException \Symfony\Component\Console\Exception\RuntimeException
14 * @expectedExceptionMessage Not enough arguments (missing: "username") 14 * @expectedExceptionMessage Not enough arguments (missing: "username")
15 */ 15 */
16 public function testExportCommandWithoutUsername() 16 public function testExportCommandWithoutUsername()
@@ -55,7 +55,7 @@ class ExportCommandTest extends WallabagCoreTestCase
55 'username' => 'admin', 55 'username' => 'admin',
56 ]); 56 ]);
57 57
58 $this->assertContains('Exporting 6 entrie(s) for user « admin »... Done', $tester->getDisplay()); 58 $this->assertContains('Exporting 5 entrie(s) for user « admin »... Done', $tester->getDisplay());
59 $this->assertFileExists('admin-export.json'); 59 $this->assertFileExists('admin-export.json');
60 } 60 }
61 61
@@ -70,7 +70,7 @@ class ExportCommandTest extends WallabagCoreTestCase
70 $tester->execute([ 70 $tester->execute([
71 'command' => $command->getName(), 71 'command' => $command->getName(),
72 'username' => 'admin', 72 'username' => 'admin',
73 'filepath' => 'specialexport.json' 73 'filepath' => 'specialexport.json',
74 ]); 74 ]);
75 75
76 $this->assertFileExists('specialexport.json'); 76 $this->assertFileExists('specialexport.json');
diff --git a/tests/Wallabag/CoreBundle/Command/InstallCommandTest.php b/tests/Wallabag/CoreBundle/Command/InstallCommandTest.php
index 1bfd41d5..94fc0b94 100644
--- a/tests/Wallabag/CoreBundle/Command/InstallCommandTest.php
+++ b/tests/Wallabag/CoreBundle/Command/InstallCommandTest.php
@@ -2,8 +2,11 @@
2 2
3namespace Tests\Wallabag\CoreBundle\Command; 3namespace Tests\Wallabag\CoreBundle\Command;
4 4
5use DAMA\DoctrineTestBundle\Doctrine\DBAL\StaticDriver;
5use Doctrine\Bundle\DoctrineBundle\Command\CreateDatabaseDoctrineCommand; 6use Doctrine\Bundle\DoctrineBundle\Command\CreateDatabaseDoctrineCommand;
6use Doctrine\Bundle\DoctrineBundle\Command\DropDatabaseDoctrineCommand; 7use Doctrine\Bundle\DoctrineBundle\Command\DropDatabaseDoctrineCommand;
8use Doctrine\DBAL\Platforms\PostgreSqlPlatform;
9use Doctrine\DBAL\Platforms\SqlitePlatform;
7use Symfony\Bundle\FrameworkBundle\Console\Application; 10use Symfony\Bundle\FrameworkBundle\Console\Application;
8use Symfony\Component\Console\Input\ArrayInput; 11use Symfony\Component\Console\Input\ArrayInput;
9use Symfony\Component\Console\Output\NullOutput; 12use Symfony\Component\Console\Output\NullOutput;
@@ -18,7 +21,9 @@ class InstallCommandTest extends WallabagCoreTestCase
18 { 21 {
19 parent::setUp(); 22 parent::setUp();
20 23
21 if ($this->getClient()->getContainer()->get('doctrine')->getConnection()->getDriver() instanceof \Doctrine\DBAL\Driver\PDOPgSql\Driver) { 24 /** @var \Doctrine\DBAL\Connection $connection */
25 $connection = $this->getClient()->getContainer()->get('doctrine')->getConnection();
26 if ($connection->getDatabasePlatform() instanceof PostgreSqlPlatform) {
22 /* 27 /*
23 * LOG: statement: CREATE DATABASE "wallabag" 28 * LOG: statement: CREATE DATABASE "wallabag"
24 * ERROR: source database "template1" is being accessed by other users 29 * ERROR: source database "template1" is being accessed by other users
@@ -30,34 +35,44 @@ class InstallCommandTest extends WallabagCoreTestCase
30 */ 35 */
31 $this->markTestSkipped('PostgreSQL spotted: can\'t find a good way to drop current database, skipping.'); 36 $this->markTestSkipped('PostgreSQL spotted: can\'t find a good way to drop current database, skipping.');
32 } 37 }
33 }
34 38
35 /** 39 if ($connection->getDatabasePlatform() instanceof SqlitePlatform) {
36 * Ensure next tests will have a clean database. 40 // Environnement variable useful only for sqlite to avoid the error "attempt to write a readonly database"
37 */ 41 // We can't define always this environnement variable because pdo_mysql seems to use it
38 public static function tearDownAfterClass() 42 // and we have the error:
39 { 43 // SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax;
40 $application = new Application(static::$kernel); 44 // check the manual that corresponds to your MariaDB server version for the right syntax to use
41 $application->setAutoExit(false); 45 // near '/tmp/wallabag_testTYj1kp' at line 1
46 $databasePath = tempnam(sys_get_temp_dir(), 'wallabag_test');
47 putenv("TEST_DATABASE_PATH=$databasePath");
48
49 // The environnement has been changed, recreate the client in order to update connection
50 parent::setUp();
51 }
42 52
43 $application->run(new ArrayInput([ 53 // disable doctrine-test-bundle
44 'command' => 'doctrine:schema:drop', 54 StaticDriver::setKeepStaticConnections(false);
45 '--no-interaction' => true,
46 '--force' => true,
47 '--env' => 'test',
48 ]), new NullOutput());
49 55
50 $application->run(new ArrayInput([ 56 $this->resetDatabase($this->getClient());
51 'command' => 'doctrine:schema:create', 57 }
52 '--no-interaction' => true,
53 '--env' => 'test',
54 ]), new NullOutput());
55 58
56 $application->run(new ArrayInput([ 59 public function tearDown()
57 'command' => 'doctrine:fixtures:load', 60 {
58 '--no-interaction' => true, 61 $databasePath = getenv('TEST_DATABASE_PATH');
59 '--env' => 'test', 62 // Remove variable environnement
60 ]), new NullOutput()); 63 putenv('TEST_DATABASE_PATH');
64 if ($databasePath && file_exists($databasePath)) {
65 unlink($databasePath);
66 } else {
67 // Create a new client to avoid the error:
68 // Transaction commit failed because the transaction has been marked for rollback only.
69 $client = static::createClient();
70 $this->resetDatabase($client);
71 }
72
73 // enable doctrine-test-bundle
74 StaticDriver::setKeepStaticConnections(true);
75 parent::tearDown();
61 } 76 }
62 77
63 public function testRunInstallCommand() 78 public function testRunInstallCommand()
@@ -67,18 +82,14 @@ class InstallCommandTest extends WallabagCoreTestCase
67 82
68 $command = $application->find('wallabag:install'); 83 $command = $application->find('wallabag:install');
69 84
70 // We mock the QuestionHelper
71 $question = $this->getMockBuilder('Symfony\Component\Console\Helper\QuestionHelper')
72 ->disableOriginalConstructor()
73 ->getMock();
74 $question->expects($this->any())
75 ->method('ask')
76 ->will($this->returnValue('yes_'.uniqid('', true)));
77
78 // We override the standard helper with our mock
79 $command->getHelperSet()->set($question, 'question');
80
81 $tester = new CommandTester($command); 85 $tester = new CommandTester($command);
86 $tester->setInputs([
87 'y', // dropping database
88 'y', // create super admin
89 'username_'.uniqid('', true), // username
90 'password_'.uniqid('', true), // password
91 'email_'.uniqid('', true).'@wallabag.it', // email
92 ]);
82 $tester->execute([ 93 $tester->execute([
83 'command' => $command->getName(), 94 'command' => $command->getName(),
84 ]); 95 ]);
@@ -87,6 +98,7 @@ class InstallCommandTest extends WallabagCoreTestCase
87 $this->assertContains('Setting up database.', $tester->getDisplay()); 98 $this->assertContains('Setting up database.', $tester->getDisplay());
88 $this->assertContains('Administration setup.', $tester->getDisplay()); 99 $this->assertContains('Administration setup.', $tester->getDisplay());
89 $this->assertContains('Config setup.', $tester->getDisplay()); 100 $this->assertContains('Config setup.', $tester->getDisplay());
101 $this->assertContains('Run migrations.', $tester->getDisplay());
90 } 102 }
91 103
92 public function testRunInstallCommandWithReset() 104 public function testRunInstallCommandWithReset()
@@ -96,18 +108,13 @@ class InstallCommandTest extends WallabagCoreTestCase
96 108
97 $command = $application->find('wallabag:install'); 109 $command = $application->find('wallabag:install');
98 110
99 // We mock the QuestionHelper
100 $question = $this->getMockBuilder('Symfony\Component\Console\Helper\QuestionHelper')
101 ->disableOriginalConstructor()
102 ->getMock();
103 $question->expects($this->any())
104 ->method('ask')
105 ->will($this->returnValue('yes_'.uniqid('', true)));
106
107 // We override the standard helper with our mock
108 $command->getHelperSet()->set($question, 'question');
109
110 $tester = new CommandTester($command); 111 $tester = new CommandTester($command);
112 $tester->setInputs([
113 'y', // create super admin
114 'username_'.uniqid('', true), // username
115 'password_'.uniqid('', true), // password
116 'email_'.uniqid('', true).'@wallabag.it', // email
117 ]);
111 $tester->execute([ 118 $tester->execute([
112 'command' => $command->getName(), 119 'command' => $command->getName(),
113 '--reset' => true, 120 '--reset' => true,
@@ -115,19 +122,20 @@ class InstallCommandTest extends WallabagCoreTestCase
115 122
116 $this->assertContains('Checking system requirements.', $tester->getDisplay()); 123 $this->assertContains('Checking system requirements.', $tester->getDisplay());
117 $this->assertContains('Setting up database.', $tester->getDisplay()); 124 $this->assertContains('Setting up database.', $tester->getDisplay());
118 $this->assertContains('Droping database, creating database and schema, clearing the cache', $tester->getDisplay()); 125 $this->assertContains('Dropping database, creating database and schema, clearing the cache', $tester->getDisplay());
119 $this->assertContains('Administration setup.', $tester->getDisplay()); 126 $this->assertContains('Administration setup.', $tester->getDisplay());
120 $this->assertContains('Config setup.', $tester->getDisplay()); 127 $this->assertContains('Config setup.', $tester->getDisplay());
128 $this->assertContains('Run migrations.', $tester->getDisplay());
121 129
122 // we force to reset everything 130 // we force to reset everything
123 $this->assertContains('Droping database, creating database and schema, clearing the cache', $tester->getDisplay()); 131 $this->assertContains('Dropping database, creating database and schema, clearing the cache', $tester->getDisplay());
124 } 132 }
125 133
126 public function testRunInstallCommandWithDatabaseRemoved() 134 public function testRunInstallCommandWithDatabaseRemoved()
127 { 135 {
128 // skipped SQLite check when database is removed because while testing for the connection, 136 // skipped SQLite check when database is removed because while testing for the connection,
129 // the driver will create the file (so the database) before testing if database exist 137 // the driver will create the file (so the database) before testing if database exist
130 if ($this->getClient()->getContainer()->get('doctrine')->getConnection()->getDriver() instanceof \Doctrine\DBAL\Driver\PDOSqlite\Driver) { 138 if ($this->getClient()->getContainer()->get('doctrine')->getConnection()->getDatabasePlatform() instanceof \Doctrine\DBAL\Platforms\SqlitePlatform) {
131 $this->markTestSkipped('SQLite spotted: can\'t test with database removed.'); 139 $this->markTestSkipped('SQLite spotted: can\'t test with database removed.');
132 } 140 }
133 141
@@ -148,18 +156,13 @@ class InstallCommandTest extends WallabagCoreTestCase
148 156
149 $command = $application->find('wallabag:install'); 157 $command = $application->find('wallabag:install');
150 158
151 // We mock the QuestionHelper
152 $question = $this->getMockBuilder('Symfony\Component\Console\Helper\QuestionHelper')
153 ->disableOriginalConstructor()
154 ->getMock();
155 $question->expects($this->any())
156 ->method('ask')
157 ->will($this->returnValue('yes_'.uniqid('', true)));
158
159 // We override the standard helper with our mock
160 $command->getHelperSet()->set($question, 'question');
161
162 $tester = new CommandTester($command); 159 $tester = new CommandTester($command);
160 $tester->setInputs([
161 'y', // create super admin
162 'username_'.uniqid('', true), // username
163 'password_'.uniqid('', true), // password
164 'email_'.uniqid('', true).'@wallabag.it', // email
165 ]);
163 $tester->execute([ 166 $tester->execute([
164 'command' => $command->getName(), 167 'command' => $command->getName(),
165 ]); 168 ]);
@@ -168,6 +171,7 @@ class InstallCommandTest extends WallabagCoreTestCase
168 $this->assertContains('Setting up database.', $tester->getDisplay()); 171 $this->assertContains('Setting up database.', $tester->getDisplay());
169 $this->assertContains('Administration setup.', $tester->getDisplay()); 172 $this->assertContains('Administration setup.', $tester->getDisplay());
170 $this->assertContains('Config setup.', $tester->getDisplay()); 173 $this->assertContains('Config setup.', $tester->getDisplay());
174 $this->assertContains('Run migrations.', $tester->getDisplay());
171 175
172 // the current database doesn't already exist 176 // the current database doesn't already exist
173 $this->assertContains('Creating database and schema, clearing the cache', $tester->getDisplay()); 177 $this->assertContains('Creating database and schema, clearing the cache', $tester->getDisplay());
@@ -180,23 +184,12 @@ class InstallCommandTest extends WallabagCoreTestCase
180 184
181 $command = $application->find('wallabag:install'); 185 $command = $application->find('wallabag:install');
182 186
183 // We mock the QuestionHelper
184 $question = $this->getMockBuilder('Symfony\Component\Console\Helper\QuestionHelper')
185 ->disableOriginalConstructor()
186 ->getMock();
187
188 $question->expects($this->exactly(3))
189 ->method('ask')
190 ->will($this->onConsecutiveCalls(
191 false, // don't want to reset the entire database
192 true, // do want to reset the schema
193 false // don't want to create a new user
194 ));
195
196 // We override the standard helper with our mock
197 $command->getHelperSet()->set($question, 'question');
198
199 $tester = new CommandTester($command); 187 $tester = new CommandTester($command);
188 $tester->setInputs([
189 'n', // don't want to reset the entire database
190 'y', // do want to reset the schema
191 'n', // don't want to create a new user
192 ]);
200 $tester->execute([ 193 $tester->execute([
201 'command' => $command->getName(), 194 'command' => $command->getName(),
202 ]); 195 ]);
@@ -205,8 +198,9 @@ class InstallCommandTest extends WallabagCoreTestCase
205 $this->assertContains('Setting up database.', $tester->getDisplay()); 198 $this->assertContains('Setting up database.', $tester->getDisplay());
206 $this->assertContains('Administration setup.', $tester->getDisplay()); 199 $this->assertContains('Administration setup.', $tester->getDisplay());
207 $this->assertContains('Config setup.', $tester->getDisplay()); 200 $this->assertContains('Config setup.', $tester->getDisplay());
201 $this->assertContains('Run migrations.', $tester->getDisplay());
208 202
209 $this->assertContains('Droping schema and creating schema', $tester->getDisplay()); 203 $this->assertContains('Dropping schema and creating schema', $tester->getDisplay());
210 } 204 }
211 205
212 public function testRunInstallCommandChooseNothing() 206 public function testRunInstallCommandChooseNothing()
@@ -235,22 +229,11 @@ class InstallCommandTest extends WallabagCoreTestCase
235 229
236 $command = $application->find('wallabag:install'); 230 $command = $application->find('wallabag:install');
237 231
238 // We mock the QuestionHelper
239 $question = $this->getMockBuilder('Symfony\Component\Console\Helper\QuestionHelper')
240 ->disableOriginalConstructor()
241 ->getMock();
242
243 $question->expects($this->exactly(2))
244 ->method('ask')
245 ->will($this->onConsecutiveCalls(
246 false, // don't want to reset the entire database
247 false // don't want to create a new user
248 ));
249
250 // We override the standard helper with our mock
251 $command->getHelperSet()->set($question, 'question');
252
253 $tester = new CommandTester($command); 232 $tester = new CommandTester($command);
233 $tester->setInputs([
234 'n', // don't want to reset the entire database
235 'n', // don't want to create a new user
236 ]);
254 $tester->execute([ 237 $tester->execute([
255 'command' => $command->getName(), 238 'command' => $command->getName(),
256 ]); 239 ]);
@@ -259,6 +242,7 @@ class InstallCommandTest extends WallabagCoreTestCase
259 $this->assertContains('Setting up database.', $tester->getDisplay()); 242 $this->assertContains('Setting up database.', $tester->getDisplay());
260 $this->assertContains('Administration setup.', $tester->getDisplay()); 243 $this->assertContains('Administration setup.', $tester->getDisplay());
261 $this->assertContains('Config setup.', $tester->getDisplay()); 244 $this->assertContains('Config setup.', $tester->getDisplay());
245 $this->assertContains('Run migrations.', $tester->getDisplay());
262 246
263 $this->assertContains('Creating schema', $tester->getDisplay()); 247 $this->assertContains('Creating schema', $tester->getDisplay());
264 } 248 }
@@ -270,26 +254,17 @@ class InstallCommandTest extends WallabagCoreTestCase
270 254
271 $command = $application->find('wallabag:install'); 255 $command = $application->find('wallabag:install');
272 256
273 // We mock the QuestionHelper
274 $question = $this->getMockBuilder('Symfony\Component\Console\Helper\QuestionHelper')
275 ->disableOriginalConstructor()
276 ->getMock();
277 $question->expects($this->any())
278 ->method('ask')
279 ->will($this->returnValue('yes_'.uniqid('', true)));
280
281 // We override the standard helper with our mock
282 $command->getHelperSet()->set($question, 'question');
283
284 $tester = new CommandTester($command); 257 $tester = new CommandTester($command);
285 $tester->execute([ 258 $tester->execute([
286 'command' => $command->getName(), 259 'command' => $command->getName(),
287 '--no-interaction' => true, 260 ], [
261 'interactive' => false,
288 ]); 262 ]);
289 263
290 $this->assertContains('Checking system requirements.', $tester->getDisplay()); 264 $this->assertContains('Checking system requirements.', $tester->getDisplay());
291 $this->assertContains('Setting up database.', $tester->getDisplay()); 265 $this->assertContains('Setting up database.', $tester->getDisplay());
292 $this->assertContains('Administration setup.', $tester->getDisplay()); 266 $this->assertContains('Administration setup.', $tester->getDisplay());
293 $this->assertContains('Config setup.', $tester->getDisplay()); 267 $this->assertContains('Config setup.', $tester->getDisplay());
268 $this->assertContains('Run migrations.', $tester->getDisplay());
294 } 269 }
295} 270}
diff --git a/tests/Wallabag/CoreBundle/Command/ShowUserCommandTest.php b/tests/Wallabag/CoreBundle/Command/ShowUserCommandTest.php
new file mode 100644
index 00000000..c0a4acfa
--- /dev/null
+++ b/tests/Wallabag/CoreBundle/Command/ShowUserCommandTest.php
@@ -0,0 +1,93 @@
1<?php
2
3namespace Tests\Wallabag\CoreBundle\Command;
4
5use Symfony\Bundle\FrameworkBundle\Console\Application;
6use Symfony\Component\Console\Tester\CommandTester;
7use Tests\Wallabag\CoreBundle\WallabagCoreTestCase;
8use Wallabag\CoreBundle\Command\ShowUserCommand;
9use Wallabag\UserBundle\Entity\User;
10
11class ShowUserCommandTest extends WallabagCoreTestCase
12{
13 /**
14 * @expectedException \Symfony\Component\Console\Exception\RuntimeException
15 * @expectedExceptionMessage Not enough arguments
16 */
17 public function testRunShowUserCommandWithoutUsername()
18 {
19 $application = new Application($this->getClient()->getKernel());
20 $application->add(new ShowUserCommand());
21
22 $command = $application->find('wallabag:user:show');
23
24 $tester = new CommandTester($command);
25 $tester->execute([
26 'command' => $command->getName(),
27 ]);
28 }
29
30 public function testRunShowUserCommandWithBadUsername()
31 {
32 $application = new Application($this->getClient()->getKernel());
33 $application->add(new ShowUserCommand());
34
35 $command = $application->find('wallabag:user:show');
36
37 $tester = new CommandTester($command);
38 $tester->execute([
39 'command' => $command->getName(),
40 'username' => 'unknown',
41 ]);
42
43 $this->assertContains('User "unknown" not found', $tester->getDisplay());
44 }
45
46 public function testRunShowUserCommandForUser()
47 {
48 $application = new Application($this->getClient()->getKernel());
49 $application->add(new ShowUserCommand());
50
51 $command = $application->find('wallabag:user:show');
52
53 $tester = new CommandTester($command);
54 $tester->execute([
55 'command' => $command->getName(),
56 'username' => 'admin',
57 ]);
58
59 $this->assertContains('Username : admin', $tester->getDisplay());
60 $this->assertContains('Email : bigboss@wallabag.org', $tester->getDisplay());
61 $this->assertContains('Display name : Big boss', $tester->getDisplay());
62 $this->assertContains('2FA activated: no', $tester->getDisplay());
63 }
64
65 public function testShowUser()
66 {
67 $client = $this->getClient();
68 $em = $client->getContainer()->get('doctrine.orm.entity_manager');
69
70 $this->logInAs('admin');
71
72 /** @var User $user */
73 $user = $em->getRepository('WallabagUserBundle:User')->findOneById($this->getLoggedInUserId());
74
75 $user->setName('Bug boss');
76 $em->persist($user);
77
78 $em->flush();
79
80 $application = new Application($this->getClient()->getKernel());
81 $application->add(new ShowUserCommand());
82
83 $command = $application->find('wallabag:user:show');
84
85 $tester = new CommandTester($command);
86 $tester->execute([
87 'command' => $command->getName(),
88 'username' => 'admin',
89 ]);
90
91 $this->assertContains('Display name : Bug boss', $tester->getDisplay());
92 }
93}
diff --git a/tests/Wallabag/CoreBundle/Command/TagAllCommandTest.php b/tests/Wallabag/CoreBundle/Command/TagAllCommandTest.php
index ec31708f..4cde3679 100644
--- a/tests/Wallabag/CoreBundle/Command/TagAllCommandTest.php
+++ b/tests/Wallabag/CoreBundle/Command/TagAllCommandTest.php
@@ -10,7 +10,7 @@ use Tests\Wallabag\CoreBundle\WallabagCoreTestCase;
10class TagAllCommandTest extends WallabagCoreTestCase 10class TagAllCommandTest extends WallabagCoreTestCase
11{ 11{
12 /** 12 /**
13 * @expectedException Symfony\Component\Console\Exception\RuntimeException 13 * @expectedException \Symfony\Component\Console\Exception\RuntimeException
14 * @expectedExceptionMessage Not enough arguments (missing: "username") 14 * @expectedExceptionMessage Not enough arguments (missing: "username")
15 */ 15 */
16 public function testRunTagAllCommandWithoutUsername() 16 public function testRunTagAllCommandWithoutUsername()
diff --git a/tests/Wallabag/CoreBundle/Controller/ConfigControllerTest.php b/tests/Wallabag/CoreBundle/Controller/ConfigControllerTest.php
index beb0598a..5bc815ee 100644
--- a/tests/Wallabag/CoreBundle/Controller/ConfigControllerTest.php
+++ b/tests/Wallabag/CoreBundle/Controller/ConfigControllerTest.php
@@ -67,8 +67,17 @@ class ConfigControllerTest extends WallabagCoreTestCase
67 public function testChangeReadingSpeed() 67 public function testChangeReadingSpeed()
68 { 68 {
69 $this->logInAs('admin'); 69 $this->logInAs('admin');
70 $this->useTheme('baggy');
70 $client = $this->getClient(); 71 $client = $this->getClient();
71 72
73 $entry = new Entry($this->getLoggedInUser());
74 $entry->setUrl('http://0.0.0.0/test-entry1')
75 ->setReadingTime(22);
76 $this->getEntityManager()->persist($entry);
77
78 $this->getEntityManager()->flush();
79 $this->getEntityManager()->clear();
80
72 $crawler = $client->request('GET', '/unread/list'); 81 $crawler = $client->request('GET', '/unread/list');
73 $form = $crawler->filter('button[id=submit-filter]')->form(); 82 $form = $crawler->filter('button[id=submit-filter]')->form();
74 $dataFilters = [ 83 $dataFilters = [
@@ -409,6 +418,7 @@ class ConfigControllerTest extends WallabagCoreTestCase
409 public function testTaggingRuleCreation() 418 public function testTaggingRuleCreation()
410 { 419 {
411 $this->logInAs('admin'); 420 $this->logInAs('admin');
421 $this->useTheme('baggy');
412 $client = $this->getClient(); 422 $client = $this->getClient();
413 423
414 $crawler = $client->request('GET', '/config'); 424 $crawler = $client->request('GET', '/config');
@@ -798,11 +808,87 @@ class ConfigControllerTest extends WallabagCoreTestCase
798 808
799 $entryReset = $em 809 $entryReset = $em
800 ->getRepository('WallabagCoreBundle:Entry') 810 ->getRepository('WallabagCoreBundle:Entry')
801 ->countAllEntriesByUsername($user->getId()); 811 ->countAllEntriesByUser($user->getId());
802 812
803 $this->assertEquals(0, $entryReset, 'Entries were reset'); 813 $this->assertEquals(0, $entryReset, 'Entries were reset');
804 } 814 }
805 815
816 public function testResetArchivedEntries()
817 {
818 $this->logInAs('empty');
819 $client = $this->getClient();
820
821 $em = $client->getContainer()->get('doctrine.orm.entity_manager');
822
823 $user = static::$kernel->getContainer()->get('security.token_storage')->getToken()->getUser();
824
825 $tag = new Tag();
826 $tag->setLabel('super');
827 $em->persist($tag);
828
829 $entry = new Entry($user);
830 $entry->setUrl('http://www.lemonde.fr/europe/article/2016/10/01/pour-le-psoe-chaque-election-s-est-transformee-en-une-agonie_5006476_3214.html');
831 $entry->setContent('Youhou');
832 $entry->setTitle('Youhou');
833 $entry->addTag($tag);
834 $em->persist($entry);
835
836 $annotation = new Annotation($user);
837 $annotation->setText('annotated');
838 $annotation->setQuote('annotated');
839 $annotation->setRanges([]);
840 $annotation->setEntry($entry);
841 $em->persist($annotation);
842
843 $tagArchived = new Tag();
844 $tagArchived->setLabel('super');
845 $em->persist($tagArchived);
846
847 $entryArchived = new Entry($user);
848 $entryArchived->setUrl('http://www.lemonde.fr/europe/article/2016/10/01/pour-le-psoe-chaque-election-s-est-transformee-en-une-agonie_5006476_3214.html');
849 $entryArchived->setContent('Youhou');
850 $entryArchived->setTitle('Youhou');
851 $entryArchived->addTag($tagArchived);
852 $entryArchived->setArchived(true);
853 $em->persist($entryArchived);
854
855 $annotationArchived = new Annotation($user);
856 $annotationArchived->setText('annotated');
857 $annotationArchived->setQuote('annotated');
858 $annotationArchived->setRanges([]);
859 $annotationArchived->setEntry($entryArchived);
860 $em->persist($annotationArchived);
861
862 $em->flush();
863
864 $crawler = $client->request('GET', '/config#set3');
865
866 $this->assertEquals(200, $client->getResponse()->getStatusCode());
867
868 $crawler = $client->click($crawler->selectLink('config.reset.archived')->link());
869
870 $this->assertEquals(302, $client->getResponse()->getStatusCode());
871 $this->assertContains('flashes.config.notice.archived_reset', $client->getContainer()->get('session')->getFlashBag()->get('notice')[0]);
872
873 $entryReset = $em
874 ->getRepository('WallabagCoreBundle:Entry')
875 ->countAllEntriesByUser($user->getId());
876
877 $this->assertEquals(1, $entryReset, 'Entries were reset');
878
879 $tagReset = $em
880 ->getRepository('WallabagCoreBundle:Tag')
881 ->countAllTags($user->getId());
882
883 $this->assertEquals(1, $tagReset, 'Tags were reset');
884
885 $annotationsReset = $em
886 ->getRepository('WallabagAnnotationBundle:Annotation')
887 ->findAnnotationsByPageId($annotationArchived->getId(), $user->getId());
888
889 $this->assertEmpty($annotationsReset, 'Annotations were reset');
890 }
891
806 public function testResetEntriesCascade() 892 public function testResetEntriesCascade()
807 { 893 {
808 $this->logInAs('empty'); 894 $this->logInAs('empty');
@@ -843,7 +929,7 @@ class ConfigControllerTest extends WallabagCoreTestCase
843 929
844 $entryReset = $em 930 $entryReset = $em
845 ->getRepository('WallabagCoreBundle:Entry') 931 ->getRepository('WallabagCoreBundle:Entry')
846 ->countAllEntriesByUsername($user->getId()); 932 ->countAllEntriesByUser($user->getId());
847 933
848 $this->assertEquals(0, $entryReset, 'Entries were reset'); 934 $this->assertEquals(0, $entryReset, 'Entries were reset');
849 935
@@ -863,6 +949,7 @@ class ConfigControllerTest extends WallabagCoreTestCase
863 public function testSwitchViewMode() 949 public function testSwitchViewMode()
864 { 950 {
865 $this->logInAs('admin'); 951 $this->logInAs('admin');
952 $this->useTheme('baggy');
866 $client = $this->getClient(); 953 $client = $this->getClient();
867 954
868 $client->request('GET', '/unread/list'); 955 $client->request('GET', '/unread/list');
diff --git a/tests/Wallabag/CoreBundle/Controller/EntryControllerTest.php b/tests/Wallabag/CoreBundle/Controller/EntryControllerTest.php
index 7db4cf1f..7cf28bfe 100644
--- a/tests/Wallabag/CoreBundle/Controller/EntryControllerTest.php
+++ b/tests/Wallabag/CoreBundle/Controller/EntryControllerTest.php
@@ -5,11 +5,28 @@ namespace Tests\Wallabag\CoreBundle\Controller;
5use Tests\Wallabag\CoreBundle\WallabagCoreTestCase; 5use Tests\Wallabag\CoreBundle\WallabagCoreTestCase;
6use Wallabag\CoreBundle\Entity\Config; 6use Wallabag\CoreBundle\Entity\Config;
7use Wallabag\CoreBundle\Entity\Entry; 7use Wallabag\CoreBundle\Entity\Entry;
8use Wallabag\CoreBundle\Entity\SiteCredential;
8 9
9class EntryControllerTest extends WallabagCoreTestCase 10class EntryControllerTest extends WallabagCoreTestCase
10{ 11{
12 public $downloadImagesEnabled = false;
11 public $url = 'http://www.lemonde.fr/pixels/article/2015/03/28/plongee-dans-l-univers-d-ingress-le-jeu-de-google-aux-frontieres-du-reel_4601155_4408996.html'; 13 public $url = 'http://www.lemonde.fr/pixels/article/2015/03/28/plongee-dans-l-univers-d-ingress-le-jeu-de-google-aux-frontieres-du-reel_4601155_4408996.html';
12 14
15 /**
16 * @after
17 *
18 * Ensure download_images_enabled is disabled after each script
19 */
20 public function tearDownImagesEnabled()
21 {
22 if ($this->downloadImagesEnabled) {
23 $client = static::createClient();
24 $client->getContainer()->get('craue_config')->set('download_images_enabled', 0);
25
26 $this->downloadImagesEnabled = false;
27 }
28 }
29
13 public function testLogin() 30 public function testLogin()
14 { 31 {
15 $client = $this->getClient(); 32 $client = $this->getClient();
@@ -55,6 +72,7 @@ class EntryControllerTest extends WallabagCoreTestCase
55 public function testGetNew() 72 public function testGetNew()
56 { 73 {
57 $this->logInAs('admin'); 74 $this->logInAs('admin');
75 $this->useTheme('baggy');
58 $client = $this->getClient(); 76 $client = $this->getClient();
59 77
60 $crawler = $client->request('GET', '/new'); 78 $crawler = $client->request('GET', '/new');
@@ -68,6 +86,7 @@ class EntryControllerTest extends WallabagCoreTestCase
68 public function testPostNewViaBookmarklet() 86 public function testPostNewViaBookmarklet()
69 { 87 {
70 $this->logInAs('admin'); 88 $this->logInAs('admin');
89 $this->useTheme('baggy');
71 $client = $this->getClient(); 90 $client = $this->getClient();
72 91
73 $crawler = $client->request('GET', '/'); 92 $crawler = $client->request('GET', '/');
@@ -135,14 +154,58 @@ class EntryControllerTest extends WallabagCoreTestCase
135 ->getRepository('WallabagCoreBundle:Entry') 154 ->getRepository('WallabagCoreBundle:Entry')
136 ->findByUrlAndUserId($this->url, $this->getLoggedInUserId()); 155 ->findByUrlAndUserId($this->url, $this->getLoggedInUserId());
137 156
157 $author = $content->getPublishedBy();
158
138 $this->assertInstanceOf('Wallabag\CoreBundle\Entity\Entry', $content); 159 $this->assertInstanceOf('Wallabag\CoreBundle\Entity\Entry', $content);
139 $this->assertEquals($this->url, $content->getUrl()); 160 $this->assertEquals($this->url, $content->getUrl());
140 $this->assertContains('Google', $content->getTitle()); 161 $this->assertContains('Google', $content->getTitle());
162 $this->assertEquals('fr', $content->getLanguage());
163 $this->assertEquals('2015-03-28 15:37:39', $content->getPublishedAt()->format('Y-m-d H:i:s'));
164 $this->assertEquals('Morgane Tual', $author[0]);
165 $this->assertArrayHasKey('x-varnish1', $content->getHeaders());
166 }
167
168 public function testPostWithMultipleAuthors()
169 {
170 $url = 'http://www.liberation.fr/planete/2017/04/05/donald-trump-et-xi-jinping-tentative-de-flirt-en-floride_1560768';
171 $this->logInAs('admin');
172 $client = $this->getClient();
173
174 $crawler = $client->request('GET', '/new');
175
176 $this->assertEquals(200, $client->getResponse()->getStatusCode());
177
178 $form = $crawler->filter('form[name=entry]')->form();
179
180 $data = [
181 'entry[url]' => $url,
182 ];
183
184 $client->submit($form, $data);
185
186 $this->assertEquals(302, $client->getResponse()->getStatusCode());
187
188 $content = $client->getContainer()
189 ->get('doctrine.orm.entity_manager')
190 ->getRepository('WallabagCoreBundle:Entry')
191 ->findByUrlAndUserId($url, $this->getLoggedInUserId());
192
193 $authors = $content->getPublishedBy();
194 $this->assertEquals('2017-04-05 19:26:13', $content->getPublishedAt()->format('Y-m-d H:i:s'));
195 $this->assertEquals('fr', $content->getLanguage());
196 $this->assertEquals('Raphaël Balenieri, correspondant à Pékin', $authors[0]);
197 $this->assertEquals('Frédéric Autran, correspondant à New York', $authors[1]);
141 } 198 }
142 199
143 public function testPostNewOkUrlExist() 200 public function testPostNewOkUrlExist()
144 { 201 {
145 $this->logInAs('admin'); 202 $this->logInAs('admin');
203
204 $entry = new Entry($this->getLoggedInUser());
205 $entry->setUrl($this->url);
206 $this->getEntityManager()->persist($entry);
207 $this->getEntityManager()->flush();
208
146 $client = $this->getClient(); 209 $client = $this->getClient();
147 210
148 $crawler = $client->request('GET', '/new'); 211 $crawler = $client->request('GET', '/new');
@@ -194,15 +257,6 @@ class EntryControllerTest extends WallabagCoreTestCase
194 257
195 $this->assertEquals(302, $client->getResponse()->getStatusCode()); 258 $this->assertEquals(302, $client->getResponse()->getStatusCode());
196 $this->assertContains('/view/', $client->getResponse()->getTargetUrl()); 259 $this->assertContains('/view/', $client->getResponse()->getTargetUrl());
197
198 $em = $client->getContainer()
199 ->get('doctrine.orm.entity_manager');
200 $entry = $em
201 ->getRepository('WallabagCoreBundle:Entry')
202 ->findOneByUrl(urldecode($url));
203
204 $em->remove($entry);
205 $em->flush();
206 } 260 }
207 261
208 /** 262 /**
@@ -235,8 +289,9 @@ class EntryControllerTest extends WallabagCoreTestCase
235 ->findOneByUrl($url); 289 ->findOneByUrl($url);
236 $tags = $entry->getTags(); 290 $tags = $entry->getTags();
237 291
238 $this->assertCount(1, $tags); 292 $this->assertCount(2, $tags);
239 $this->assertEquals('wallabag', $tags[0]->getLabel()); 293 $this->assertContains('wallabag', $tags);
294 $this->assertEquals('en', $entry->getLanguage());
240 295
241 $em->remove($entry); 296 $em->remove($entry);
242 $em->flush(); 297 $em->flush();
@@ -264,8 +319,8 @@ class EntryControllerTest extends WallabagCoreTestCase
264 319
265 $tags = $entry->getTags(); 320 $tags = $entry->getTags();
266 321
267 $this->assertCount(1, $tags); 322 $this->assertCount(2, $tags);
268 $this->assertEquals('wallabag', $tags[0]->getLabel()); 323 $this->assertContains('wallabag', $tags);
269 324
270 $em->remove($entry); 325 $em->remove($entry);
271 $em->flush(); 326 $em->flush();
@@ -312,29 +367,26 @@ class EntryControllerTest extends WallabagCoreTestCase
312 $this->assertEquals('/all/list', $client->getResponse()->getTargetUrl()); 367 $this->assertEquals('/all/list', $client->getResponse()->getTargetUrl());
313 } 368 }
314 369
315 /**
316 * @depends testPostNewOk
317 */
318 public function testView() 370 public function testView()
319 { 371 {
320 $this->logInAs('admin'); 372 $this->logInAs('admin');
321 $client = $this->getClient(); 373 $client = $this->getClient();
322 374
323 $content = $client->getContainer() 375 $entry = new Entry($this->getLoggedInUser());
324 ->get('doctrine.orm.entity_manager') 376 $entry->setUrl('http://example.com/foo');
325 ->getRepository('WallabagCoreBundle:Entry') 377 $entry->setTitle('title foo');
326 ->findByUrlAndUserId($this->url, $this->getLoggedInUserId()); 378 $entry->setContent('foo bar baz');
379 $this->getEntityManager()->persist($entry);
380 $this->getEntityManager()->flush();
327 381
328 $crawler = $client->request('GET', '/view/'.$content->getId()); 382 $crawler = $client->request('GET', '/view/'.$entry->getId());
329 383
330 $this->assertEquals(200, $client->getResponse()->getStatusCode()); 384 $this->assertEquals(200, $client->getResponse()->getStatusCode());
331 $this->assertGreaterThan(1, $body = $crawler->filter('body')->extract(['_text'])); 385 $this->assertGreaterThan(1, $body = $crawler->filter('body')->extract(['_text']));
332 $this->assertContains($content->getTitle(), $body[0]); 386 $this->assertContains($entry->getTitle(), $body[0]);
333 } 387 }
334 388
335 /** 389 /**
336 * @depends testPostNewOk
337 *
338 * This test will require an internet connection. 390 * This test will require an internet connection.
339 */ 391 */
340 public function testReload() 392 public function testReload()
@@ -342,63 +394,45 @@ class EntryControllerTest extends WallabagCoreTestCase
342 $this->logInAs('admin'); 394 $this->logInAs('admin');
343 $client = $this->getClient(); 395 $client = $this->getClient();
344 396
345 $em = $client->getContainer() 397 $entry = new Entry($this->getLoggedInUser());
346 ->get('doctrine.orm.entity_manager'); 398 $entry->setUrl($this->url);
347 399 $entry->setTitle('title foo');
348 $content = $em 400 $entry->setContent('');
349 ->getRepository('WallabagCoreBundle:Entry') 401 $this->getEntityManager()->persist($entry);
350 ->findByUrlAndUserId($this->url, $this->getLoggedInUserId()); 402 $this->getEntityManager()->flush();
403 $this->getEntityManager()->clear();
351 404
352 // empty content 405 $client->request('GET', '/reload/'.$entry->getId());
353 $content->setContent('');
354 $em->persist($content);
355 $em->flush();
356
357 $client->request('GET', '/reload/'.$content->getId());
358 406
359 $this->assertEquals(302, $client->getResponse()->getStatusCode()); 407 $this->assertEquals(302, $client->getResponse()->getStatusCode());
360 408
361 $content = $em 409 $entry = $this->getEntityManager()
362 ->getRepository('WallabagCoreBundle:Entry') 410 ->getRepository('WallabagCoreBundle:Entry')
363 ->find($content->getId()); 411 ->find($entry->getId());
364 412
365 $this->assertNotEmpty($content->getContent()); 413 $this->assertNotEmpty($entry->getContent());
366 } 414 }
367 415
368 /**
369 * @depends testPostNewOk
370 */
371 public function testReloadWithFetchingFailed() 416 public function testReloadWithFetchingFailed()
372 { 417 {
373 $this->logInAs('admin'); 418 $this->logInAs('admin');
374 $client = $this->getClient(); 419 $client = $this->getClient();
375 420
376 $em = $client->getContainer() 421 $entry = new Entry($this->getLoggedInUser());
377 ->get('doctrine.orm.entity_manager'); 422 $entry->setUrl('http://0.0.0.0/failed.html');
378 423 $this->getEntityManager()->persist($entry);
379 $content = $em 424 $this->getEntityManager()->flush();
380 ->getRepository('WallabagCoreBundle:Entry')
381 ->findByUrlAndUserId($this->url, $this->getLoggedInUserId());
382
383 // put a known failed url
384 $content->setUrl('http://0.0.0.0/failed.html');
385 $em->persist($content);
386 $em->flush();
387 425
388 $client->request('GET', '/reload/'.$content->getId()); 426 $client->request('GET', '/reload/'.$entry->getId());
389 427
390 $this->assertEquals(302, $client->getResponse()->getStatusCode()); 428 $this->assertEquals(302, $client->getResponse()->getStatusCode());
391 429
392 // force EntityManager to clear previous entity 430 // force EntityManager to clear previous entity
393 // otherwise, retrieve the same entity will retrieve change from the previous request :0 431 // otherwise, retrieve the same entity will retrieve change from the previous request :0
394 $em->clear(); 432 $this->getEntityManager()->clear();
395 $newContent = $em 433 $newContent = $this->getEntityManager()
396 ->getRepository('WallabagCoreBundle:Entry') 434 ->getRepository('WallabagCoreBundle:Entry')
397 ->find($content->getId()); 435 ->find($entry->getId());
398
399 $newContent->setUrl($this->url);
400 $em->persist($newContent);
401 $em->flush();
402 436
403 $this->assertNotEquals($client->getContainer()->getParameter('wallabag_core.fetching_error_message'), $newContent->getContent()); 437 $this->assertNotEquals($client->getContainer()->getParameter('wallabag_core.fetching_error_message'), $newContent->getContent());
404 } 438 }
@@ -408,12 +442,12 @@ class EntryControllerTest extends WallabagCoreTestCase
408 $this->logInAs('admin'); 442 $this->logInAs('admin');
409 $client = $this->getClient(); 443 $client = $this->getClient();
410 444
411 $content = $client->getContainer() 445 $entry = new Entry($this->getLoggedInUser());
412 ->get('doctrine.orm.entity_manager') 446 $entry->setUrl($this->url);
413 ->getRepository('WallabagCoreBundle:Entry') 447 $this->getEntityManager()->persist($entry);
414 ->findByUrlAndUserId($this->url, $this->getLoggedInUserId()); 448 $this->getEntityManager()->flush();
415 449
416 $crawler = $client->request('GET', '/edit/'.$content->getId()); 450 $crawler = $client->request('GET', '/edit/'.$entry->getId());
417 451
418 $this->assertEquals(200, $client->getResponse()->getStatusCode()); 452 $this->assertEquals(200, $client->getResponse()->getStatusCode());
419 453
@@ -426,12 +460,12 @@ class EntryControllerTest extends WallabagCoreTestCase
426 $this->logInAs('admin'); 460 $this->logInAs('admin');
427 $client = $this->getClient(); 461 $client = $this->getClient();
428 462
429 $content = $client->getContainer() 463 $entry = new Entry($this->getLoggedInUser());
430 ->get('doctrine.orm.entity_manager') 464 $entry->setUrl($this->url);
431 ->getRepository('WallabagCoreBundle:Entry') 465 $this->getEntityManager()->persist($entry);
432 ->findByUrlAndUserId($this->url, $this->getLoggedInUserId()); 466 $this->getEntityManager()->flush();
433 467
434 $crawler = $client->request('GET', '/edit/'.$content->getId()); 468 $crawler = $client->request('GET', '/edit/'.$entry->getId());
435 469
436 $this->assertEquals(200, $client->getResponse()->getStatusCode()); 470 $this->assertEquals(200, $client->getResponse()->getStatusCode());
437 471
@@ -456,19 +490,20 @@ class EntryControllerTest extends WallabagCoreTestCase
456 $this->logInAs('admin'); 490 $this->logInAs('admin');
457 $client = $this->getClient(); 491 $client = $this->getClient();
458 492
459 $content = $client->getContainer() 493 $entry = new Entry($this->getLoggedInUser());
460 ->get('doctrine.orm.entity_manager') 494 $entry->setUrl($this->url);
461 ->getRepository('WallabagCoreBundle:Entry') 495 $this->getEntityManager()->persist($entry);
462 ->findByUrlAndUserId($this->url, $this->getLoggedInUserId()); 496 $this->getEntityManager()->flush();
497 $this->getEntityManager()->clear();
463 498
464 $client->request('GET', '/archive/'.$content->getId()); 499 $client->request('GET', '/archive/'.$entry->getId());
465 500
466 $this->assertEquals(302, $client->getResponse()->getStatusCode()); 501 $this->assertEquals(302, $client->getResponse()->getStatusCode());
467 502
468 $res = $client->getContainer() 503 $res = $client->getContainer()
469 ->get('doctrine.orm.entity_manager') 504 ->get('doctrine.orm.entity_manager')
470 ->getRepository('WallabagCoreBundle:Entry') 505 ->getRepository('WallabagCoreBundle:Entry')
471 ->find($content->getId()); 506 ->find($entry->getId());
472 507
473 $this->assertEquals($res->isArchived(), true); 508 $this->assertEquals($res->isArchived(), true);
474 } 509 }
@@ -478,19 +513,20 @@ class EntryControllerTest extends WallabagCoreTestCase
478 $this->logInAs('admin'); 513 $this->logInAs('admin');
479 $client = $this->getClient(); 514 $client = $this->getClient();
480 515
481 $content = $client->getContainer() 516 $entry = new Entry($this->getLoggedInUser());
482 ->get('doctrine.orm.entity_manager') 517 $entry->setUrl($this->url);
483 ->getRepository('WallabagCoreBundle:Entry') 518 $this->getEntityManager()->persist($entry);
484 ->findByUrlAndUserId($this->url, $this->getLoggedInUserId()); 519 $this->getEntityManager()->flush();
520 $this->getEntityManager()->clear();
485 521
486 $client->request('GET', '/star/'.$content->getId()); 522 $client->request('GET', '/star/'.$entry->getId());
487 523
488 $this->assertEquals(302, $client->getResponse()->getStatusCode()); 524 $this->assertEquals(302, $client->getResponse()->getStatusCode());
489 525
490 $res = $client->getContainer() 526 $res = $client->getContainer()
491 ->get('doctrine.orm.entity_manager') 527 ->get('doctrine.orm.entity_manager')
492 ->getRepository('WallabagCoreBundle:Entry') 528 ->getRepository('WallabagCoreBundle:Entry')
493 ->findOneById($content->getId()); 529 ->findOneById($entry->getId());
494 530
495 $this->assertEquals($res->isStarred(), true); 531 $this->assertEquals($res->isStarred(), true);
496 } 532 }
@@ -500,16 +536,16 @@ class EntryControllerTest extends WallabagCoreTestCase
500 $this->logInAs('admin'); 536 $this->logInAs('admin');
501 $client = $this->getClient(); 537 $client = $this->getClient();
502 538
503 $content = $client->getContainer() 539 $entry = new Entry($this->getLoggedInUser());
504 ->get('doctrine.orm.entity_manager') 540 $entry->setUrl($this->url);
505 ->getRepository('WallabagCoreBundle:Entry') 541 $this->getEntityManager()->persist($entry);
506 ->findByUrlAndUserId($this->url, $this->getLoggedInUserId()); 542 $this->getEntityManager()->flush();
507 543
508 $client->request('GET', '/delete/'.$content->getId()); 544 $client->request('GET', '/delete/'.$entry->getId());
509 545
510 $this->assertEquals(302, $client->getResponse()->getStatusCode()); 546 $this->assertEquals(302, $client->getResponse()->getStatusCode());
511 547
512 $client->request('GET', '/delete/'.$content->getId()); 548 $client->request('GET', '/delete/'.$entry->getId());
513 549
514 $this->assertEquals(404, $client->getResponse()->getStatusCode()); 550 $this->assertEquals(404, $client->getResponse()->getStatusCode());
515 } 551 }
@@ -575,7 +611,13 @@ class EntryControllerTest extends WallabagCoreTestCase
575 public function testFilterOnReadingTime() 611 public function testFilterOnReadingTime()
576 { 612 {
577 $this->logInAs('admin'); 613 $this->logInAs('admin');
614 $this->useTheme('baggy');
578 $client = $this->getClient(); 615 $client = $this->getClient();
616 $entry = new Entry($this->getLoggedInUser());
617 $entry->setUrl($this->url);
618 $entry->setReadingTime(22);
619 $this->getEntityManager()->persist($entry);
620 $this->getEntityManager()->flush();
579 621
580 $crawler = $client->request('GET', '/unread/list'); 622 $crawler = $client->request('GET', '/unread/list');
581 623
@@ -614,9 +656,20 @@ class EntryControllerTest extends WallabagCoreTestCase
614 public function testFilterOnReadingTimeOnlyUpper() 656 public function testFilterOnReadingTimeOnlyUpper()
615 { 657 {
616 $this->logInAs('admin'); 658 $this->logInAs('admin');
659 $this->useTheme('baggy');
617 $client = $this->getClient(); 660 $client = $this->getClient();
618 661
619 $crawler = $client->request('GET', '/unread/list'); 662 $crawler = $client->request('GET', '/all/list');
663 $this->assertCount(5, $crawler->filter('div[class=entry]'));
664
665 $entry = new Entry($this->getLoggedInUser());
666 $entry->setUrl($this->url);
667 $entry->setReadingTime(23);
668 $this->getEntityManager()->persist($entry);
669 $this->getEntityManager()->flush();
670
671 $crawler = $client->request('GET', '/all/list');
672 $this->assertCount(6, $crawler->filter('div[class=entry]'));
620 673
621 $form = $crawler->filter('button[id=submit-filter]')->form(); 674 $form = $crawler->filter('button[id=submit-filter]')->form();
622 675
@@ -626,12 +679,13 @@ class EntryControllerTest extends WallabagCoreTestCase
626 679
627 $crawler = $client->submit($form, $data); 680 $crawler = $client->submit($form, $data);
628 681
629 $this->assertCount(2, $crawler->filter('div[class=entry]')); 682 $this->assertCount(5, $crawler->filter('div[class=entry]'));
630 } 683 }
631 684
632 public function testFilterOnReadingTimeOnlyLower() 685 public function testFilterOnReadingTimeOnlyLower()
633 { 686 {
634 $this->logInAs('admin'); 687 $this->logInAs('admin');
688 $this->useTheme('baggy');
635 $client = $this->getClient(); 689 $client = $this->getClient();
636 690
637 $crawler = $client->request('GET', '/unread/list'); 691 $crawler = $client->request('GET', '/unread/list');
@@ -644,12 +698,22 @@ class EntryControllerTest extends WallabagCoreTestCase
644 698
645 $crawler = $client->submit($form, $data); 699 $crawler = $client->submit($form, $data);
646 700
647 $this->assertCount(4, $crawler->filter('div[class=entry]')); 701 $this->assertCount(0, $crawler->filter('div[class=entry]'));
702
703 $entry = new Entry($this->getLoggedInUser());
704 $entry->setUrl($this->url);
705 $entry->setReadingTime(23);
706 $this->getEntityManager()->persist($entry);
707 $this->getEntityManager()->flush();
708
709 $crawler = $client->submit($form, $data);
710 $this->assertCount(1, $crawler->filter('div[class=entry]'));
648 } 711 }
649 712
650 public function testFilterOnUnreadStatus() 713 public function testFilterOnUnreadStatus()
651 { 714 {
652 $this->logInAs('admin'); 715 $this->logInAs('admin');
716 $this->useTheme('baggy');
653 $client = $this->getClient(); 717 $client = $this->getClient();
654 718
655 $crawler = $client->request('GET', '/all/list'); 719 $crawler = $client->request('GET', '/all/list');
@@ -663,11 +727,22 @@ class EntryControllerTest extends WallabagCoreTestCase
663 $crawler = $client->submit($form, $data); 727 $crawler = $client->submit($form, $data);
664 728
665 $this->assertCount(4, $crawler->filter('div[class=entry]')); 729 $this->assertCount(4, $crawler->filter('div[class=entry]'));
730
731 $entry = new Entry($this->getLoggedInUser());
732 $entry->setUrl($this->url);
733 $entry->setArchived(false);
734 $this->getEntityManager()->persist($entry);
735 $this->getEntityManager()->flush();
736
737 $crawler = $client->submit($form, $data);
738
739 $this->assertCount(5, $crawler->filter('div[class=entry]'));
666 } 740 }
667 741
668 public function testFilterOnCreationDate() 742 public function testFilterOnCreationDate()
669 { 743 {
670 $this->logInAs('admin'); 744 $this->logInAs('admin');
745 $this->useTheme('baggy');
671 $client = $this->getClient(); 746 $client = $this->getClient();
672 747
673 $crawler = $client->request('GET', '/unread/list'); 748 $crawler = $client->request('GET', '/unread/list');
@@ -734,6 +809,7 @@ class EntryControllerTest extends WallabagCoreTestCase
734 public function testFilterOnDomainName() 809 public function testFilterOnDomainName()
735 { 810 {
736 $this->logInAs('admin'); 811 $this->logInAs('admin');
812 $this->useTheme('baggy');
737 $client = $this->getClient(); 813 $client = $this->getClient();
738 814
739 $crawler = $client->request('GET', '/unread/list'); 815 $crawler = $client->request('GET', '/unread/list');
@@ -766,6 +842,7 @@ class EntryControllerTest extends WallabagCoreTestCase
766 public function testFilterOnStatus() 842 public function testFilterOnStatus()
767 { 843 {
768 $this->logInAs('admin'); 844 $this->logInAs('admin');
845 $this->useTheme('baggy');
769 $client = $this->getClient(); 846 $client = $this->getClient();
770 847
771 $crawler = $client->request('GET', '/unread/list'); 848 $crawler = $client->request('GET', '/unread/list');
@@ -784,9 +861,24 @@ class EntryControllerTest extends WallabagCoreTestCase
784 $this->assertCount(1, $crawler->filter('div[class=entry]')); 861 $this->assertCount(1, $crawler->filter('div[class=entry]'));
785 } 862 }
786 863
864 public function testFilterOnIsPublic()
865 {
866 $this->logInAs('admin');
867 $this->useTheme('baggy');
868 $client = $this->getClient();
869
870 $crawler = $client->request('GET', '/unread/list');
871 $form = $crawler->filter('button[id=submit-filter]')->form();
872 $form['entry_filter[isPublic]']->tick();
873
874 $crawler = $client->submit($form);
875 $this->assertCount(0, $crawler->filter('div[class=entry]'));
876 }
877
787 public function testPreviewPictureFilter() 878 public function testPreviewPictureFilter()
788 { 879 {
789 $this->logInAs('admin'); 880 $this->logInAs('admin');
881 $this->useTheme('baggy');
790 $client = $this->getClient(); 882 $client = $this->getClient();
791 883
792 $crawler = $client->request('GET', '/unread/list'); 884 $crawler = $client->request('GET', '/unread/list');
@@ -800,8 +892,15 @@ class EntryControllerTest extends WallabagCoreTestCase
800 public function testFilterOnLanguage() 892 public function testFilterOnLanguage()
801 { 893 {
802 $this->logInAs('admin'); 894 $this->logInAs('admin');
895 $this->useTheme('baggy');
803 $client = $this->getClient(); 896 $client = $this->getClient();
804 897
898 $entry = new Entry($this->getLoggedInUser());
899 $entry->setUrl($this->url);
900 $entry->setLanguage('fr');
901 $this->getEntityManager()->persist($entry);
902 $this->getEntityManager()->flush();
903
805 $crawler = $client->request('GET', '/unread/list'); 904 $crawler = $client->request('GET', '/unread/list');
806 $form = $crawler->filter('button[id=submit-filter]')->form(); 905 $form = $crawler->filter('button[id=submit-filter]')->form();
807 $data = [ 906 $data = [
@@ -809,7 +908,7 @@ class EntryControllerTest extends WallabagCoreTestCase
809 ]; 908 ];
810 909
811 $crawler = $client->submit($form, $data); 910 $crawler = $client->submit($form, $data);
812 $this->assertCount(2, $crawler->filter('div[class=entry]')); 911 $this->assertCount(3, $crawler->filter('div[class=entry]'));
813 912
814 $form = $crawler->filter('button[id=submit-filter]')->form(); 913 $form = $crawler->filter('button[id=submit-filter]')->form();
815 $data = [ 914 $data = [
@@ -825,10 +924,14 @@ class EntryControllerTest extends WallabagCoreTestCase
825 $this->logInAs('admin'); 924 $this->logInAs('admin');
826 $client = $this->getClient(); 925 $client = $this->getClient();
827 926
828 $content = $client->getContainer() 927 // sharing is enabled
829 ->get('doctrine.orm.entity_manager') 928 $client->getContainer()->get('craue_config')->set('share_public', 1);
830 ->getRepository('WallabagCoreBundle:Entry') 929
831 ->findOneByUser($this->getLoggedInUserId()); 930 $content = new Entry($this->getLoggedInUser());
931 $content->setUrl($this->url);
932 $this->getEntityManager()->persist($content);
933 $this->getEntityManager()->flush();
934 $this->getEntityManager()->clear();
832 935
833 // no uid 936 // no uid
834 $client->request('GET', '/share/'.$content->getUid()); 937 $client->request('GET', '/share/'.$content->getUid());
@@ -869,6 +972,7 @@ class EntryControllerTest extends WallabagCoreTestCase
869 972
870 public function testNewEntryWithDownloadImagesEnabled() 973 public function testNewEntryWithDownloadImagesEnabled()
871 { 974 {
975 $this->downloadImagesEnabled = true;
872 $this->logInAs('admin'); 976 $this->logInAs('admin');
873 $client = $this->getClient(); 977 $client = $this->getClient();
874 978
@@ -899,7 +1003,8 @@ class EntryControllerTest extends WallabagCoreTestCase
899 $this->assertInstanceOf('Wallabag\CoreBundle\Entity\Entry', $entry); 1003 $this->assertInstanceOf('Wallabag\CoreBundle\Entity\Entry', $entry);
900 $this->assertEquals($url, $entry->getUrl()); 1004 $this->assertEquals($url, $entry->getUrl());
901 $this->assertContains('Perpignan', $entry->getTitle()); 1005 $this->assertContains('Perpignan', $entry->getTitle());
902 $this->assertContains('/d9bc0fcd.jpeg', $entry->getContent()); 1006 // instead of checking for the filename (which might change) check that the image is now local
1007 $this->assertContains('https://your-wallabag-url-instance.com/assets/images/', $entry->getContent());
903 1008
904 $client->getContainer()->get('craue_config')->set('download_images_enabled', 0); 1009 $client->getContainer()->get('craue_config')->set('download_images_enabled', 0);
905 } 1010 }
@@ -909,12 +1014,27 @@ class EntryControllerTest extends WallabagCoreTestCase
909 */ 1014 */
910 public function testRemoveEntryWithDownloadImagesEnabled() 1015 public function testRemoveEntryWithDownloadImagesEnabled()
911 { 1016 {
1017 $this->downloadImagesEnabled = true;
912 $this->logInAs('admin'); 1018 $this->logInAs('admin');
913 $client = $this->getClient(); 1019 $client = $this->getClient();
914 1020
915 $url = 'http://www.20minutes.fr/montpellier/1952003-20161030-video-car-tombe-panne-rugbymen-perpignan-improvisent-melee-route'; 1021 $url = 'http://www.20minutes.fr/montpellier/1952003-20161030-video-car-tombe-panne-rugbymen-perpignan-improvisent-melee-route';
916 $client->getContainer()->get('craue_config')->set('download_images_enabled', 1); 1022 $client->getContainer()->get('craue_config')->set('download_images_enabled', 1);
917 1023
1024 $crawler = $client->request('GET', '/new');
1025
1026 $this->assertEquals(200, $client->getResponse()->getStatusCode());
1027
1028 $form = $crawler->filter('form[name=entry]')->form();
1029
1030 $data = [
1031 'entry[url]' => $url,
1032 ];
1033
1034 $client->submit($form, $data);
1035
1036 $this->assertEquals(302, $client->getResponse()->getStatusCode());
1037
918 $content = $client->getContainer() 1038 $content = $client->getContainer()
919 ->get('doctrine.orm.entity_manager') 1039 ->get('doctrine.orm.entity_manager')
920 ->getRepository('WallabagCoreBundle:Entry') 1040 ->getRepository('WallabagCoreBundle:Entry')
@@ -932,28 +1052,19 @@ class EntryControllerTest extends WallabagCoreTestCase
932 $this->logInAs('empty'); 1052 $this->logInAs('empty');
933 $client = $this->getClient(); 1053 $client = $this->getClient();
934 1054
935 $em = $client->getContainer()->get('doctrine.orm.entity_manager');
936 $user = $em
937 ->getRepository('WallabagUserBundle:User')
938 ->find($this->getLoggedInUserId());
939
940 if (!$user) {
941 $this->markTestSkipped('No user found in db.');
942 }
943
944 // Redirect to homepage 1055 // Redirect to homepage
945 $config = $user->getConfig(); 1056 $config = $this->getLoggedInUser()->getConfig();
946 $config->setActionMarkAsRead(Config::REDIRECT_TO_HOMEPAGE); 1057 $config->setActionMarkAsRead(Config::REDIRECT_TO_HOMEPAGE);
947 $em->persist($config); 1058 $this->getEntityManager()->persist($config);
948 $em->flush();
949 1059
950 $content = $client->getContainer() 1060 $entry = new Entry($this->getLoggedInUser());
951 ->get('doctrine.orm.entity_manager') 1061 $entry->setUrl($this->url);
952 ->getRepository('WallabagCoreBundle:Entry') 1062 $this->getEntityManager()->persist($entry);
953 ->findByUrlAndUserId($this->url, $this->getLoggedInUserId());
954 1063
955 $client->request('GET', '/view/'.$content->getId()); 1064 $this->getEntityManager()->flush();
956 $client->request('GET', '/archive/'.$content->getId()); 1065
1066 $client->request('GET', '/view/'.$entry->getId());
1067 $client->request('GET', '/archive/'.$entry->getId());
957 1068
958 $this->assertEquals(302, $client->getResponse()->getStatusCode()); 1069 $this->assertEquals(302, $client->getResponse()->getStatusCode());
959 $this->assertEquals('/', $client->getResponse()->headers->get('location')); 1070 $this->assertEquals('/', $client->getResponse()->headers->get('location'));
@@ -964,46 +1075,36 @@ class EntryControllerTest extends WallabagCoreTestCase
964 $this->logInAs('empty'); 1075 $this->logInAs('empty');
965 $client = $this->getClient(); 1076 $client = $this->getClient();
966 1077
967 $em = $client->getContainer()->get('doctrine.orm.entity_manager');
968 $user = $em
969 ->getRepository('WallabagUserBundle:User')
970 ->find($this->getLoggedInUserId());
971
972 if (!$user) {
973 $this->markTestSkipped('No user found in db.');
974 }
975
976 // Redirect to current page 1078 // Redirect to current page
977 $config = $user->getConfig(); 1079 $config = $this->getLoggedInUser()->getConfig();
978 $config->setActionMarkAsRead(Config::REDIRECT_TO_CURRENT_PAGE); 1080 $config->setActionMarkAsRead(Config::REDIRECT_TO_CURRENT_PAGE);
979 $em->persist($config); 1081 $this->getEntityManager()->persist($config);
980 $em->flush();
981 1082
982 $content = $client->getContainer() 1083 $entry = new Entry($this->getLoggedInUser());
983 ->get('doctrine.orm.entity_manager') 1084 $entry->setUrl($this->url);
984 ->getRepository('WallabagCoreBundle:Entry') 1085 $this->getEntityManager()->persist($entry);
985 ->findByUrlAndUserId($this->url, $this->getLoggedInUserId());
986 1086
987 $client->request('GET', '/view/'.$content->getId()); 1087 $this->getEntityManager()->flush();
988 $client->request('GET', '/archive/'.$content->getId()); 1088
1089 $client->request('GET', '/view/'.$entry->getId());
1090 $client->request('GET', '/archive/'.$entry->getId());
989 1091
990 $this->assertEquals(302, $client->getResponse()->getStatusCode()); 1092 $this->assertEquals(302, $client->getResponse()->getStatusCode());
991 $this->assertContains('/view/'.$content->getId(), $client->getResponse()->headers->get('location')); 1093 $this->assertContains('/view/'.$entry->getId(), $client->getResponse()->headers->get('location'));
992 } 1094 }
993 1095
994 public function testFilterOnHttpStatus() 1096 public function testFilterOnHttpStatus()
995 { 1097 {
996 $this->logInAs('admin'); 1098 $this->logInAs('admin');
1099 $this->useTheme('baggy');
997 $client = $this->getClient(); 1100 $client = $this->getClient();
998 1101
999 $crawler = $client->request('GET', '/new'); 1102 $entry = new Entry($this->getLoggedInUser());
1000 $form = $crawler->filter('form[name=entry]')->form(); 1103 $entry->setUrl('http://www.lemonde.fr/incorrect-url/');
1104 $entry->setHttpStatus(404);
1105 $this->getEntityManager()->persist($entry);
1001 1106
1002 $data = [ 1107 $this->getEntityManager()->flush();
1003 'entry[url]' => 'http://www.lemonde.fr/incorrect-url/',
1004 ];
1005
1006 $client->submit($form, $data);
1007 1108
1008 $crawler = $client->request('GET', '/all/list'); 1109 $crawler = $client->request('GET', '/all/list');
1009 $form = $crawler->filter('button[id=submit-filter]')->form(); 1110 $form = $crawler->filter('button[id=submit-filter]')->form();
@@ -1016,14 +1117,17 @@ class EntryControllerTest extends WallabagCoreTestCase
1016 1117
1017 $this->assertCount(1, $crawler->filter('div[class=entry]')); 1118 $this->assertCount(1, $crawler->filter('div[class=entry]'));
1018 1119
1019 $crawler = $client->request('GET', '/new'); 1120 $entry = new Entry($this->getLoggedInUser());
1020 $form = $crawler->filter('form[name=entry]')->form(); 1121 $entry->setUrl($this->url);
1122 $entry->setHttpStatus(200);
1123 $this->getEntityManager()->persist($entry);
1021 1124
1022 $data = [ 1125 $entry = new Entry($this->getLoggedInUser());
1023 'entry[url]' => 'http://www.nextinpact.com/news/101235-wallabag-alternative-libre-a-pocket-creuse-petit-a-petit-son-nid.htm', 1126 $entry->setUrl('http://www.nextinpact.com/news/101235-wallabag-alternative-libre-a-pocket-creuse-petit-a-petit-son-nid.htm');
1024 ]; 1127 $entry->setHttpStatus(200);
1128 $this->getEntityManager()->persist($entry);
1025 1129
1026 $client->submit($form, $data); 1130 $this->getEntityManager()->flush();
1027 1131
1028 $crawler = $client->request('GET', '/all/list'); 1132 $crawler = $client->request('GET', '/all/list');
1029 $form = $crawler->filter('button[id=submit-filter]')->form(); 1133 $form = $crawler->filter('button[id=submit-filter]')->form();
@@ -1034,7 +1138,7 @@ class EntryControllerTest extends WallabagCoreTestCase
1034 1138
1035 $crawler = $client->submit($form, $data); 1139 $crawler = $client->submit($form, $data);
1036 1140
1037 $this->assertCount(1, $crawler->filter('div[class=entry]')); 1141 $this->assertCount(2, $crawler->filter('div[class=entry]'));
1038 1142
1039 $crawler = $client->request('GET', '/all/list'); 1143 $crawler = $client->request('GET', '/all/list');
1040 $form = $crawler->filter('button[id=submit-filter]')->form(); 1144 $form = $crawler->filter('button[id=submit-filter]')->form();
@@ -1045,14 +1149,21 @@ class EntryControllerTest extends WallabagCoreTestCase
1045 1149
1046 $crawler = $client->submit($form, $data); 1150 $crawler = $client->submit($form, $data);
1047 1151
1048 $this->assertCount(7, $crawler->filter('div[class=entry]')); 1152 $this->assertCount(8, $crawler->filter('div[class=entry]'));
1049 } 1153 }
1050 1154
1051 public function testSearch() 1155 public function testSearch()
1052 { 1156 {
1053 $this->logInAs('admin'); 1157 $this->logInAs('admin');
1158 $this->useTheme('baggy');
1054 $client = $this->getClient(); 1159 $client = $this->getClient();
1055 1160
1161 $entry = new Entry($this->getLoggedInUser());
1162 $entry->setUrl($this->url);
1163 $entry->setTitle('test');
1164 $this->getEntityManager()->persist($entry);
1165 $this->getEntityManager()->flush();
1166
1056 // Search on unread list 1167 // Search on unread list
1057 $crawler = $client->request('GET', '/unread/list'); 1168 $crawler = $client->request('GET', '/unread/list');
1058 1169
@@ -1063,35 +1174,37 @@ class EntryControllerTest extends WallabagCoreTestCase
1063 1174
1064 $crawler = $client->submit($form, $data); 1175 $crawler = $client->submit($form, $data);
1065 1176
1066 $this->assertCount(5, $crawler->filter('div[class=entry]')); 1177 $this->assertCount(4, $crawler->filter('div[class=entry]'));
1067 1178
1068 // Search on starred list 1179 // Search on starred list
1069 $crawler = $client->request('GET', '/starred/list'); 1180 $crawler = $client->request('GET', '/starred/list');
1070 1181
1182 $entry = new Entry($this->getLoggedInUser());
1183 $entry->setUrl('http://localhost/foo/bar');
1184 $entry->setTitle('testeur');
1185 $entry->setStarred(true);
1186 $this->getEntityManager()->persist($entry);
1187 $this->getEntityManager()->flush();
1188
1071 $form = $crawler->filter('form[name=search]')->form(); 1189 $form = $crawler->filter('form[name=search]')->form();
1072 $data = [ 1190 $data = [
1073 'search_entry[term]' => 'title', 1191 'search_entry[term]' => 'testeur',
1074 ]; 1192 ];
1075 1193
1076 $crawler = $client->submit($form, $data); 1194 $crawler = $client->submit($form, $data);
1077 1195
1078 $this->assertCount(1, $crawler->filter('div[class=entry]')); 1196 $this->assertCount(1, $crawler->filter('div[class=entry]'));
1079 1197
1080 // Added new article to test on archive list
1081 $crawler = $client->request('GET', '/new');
1082 $form = $crawler->filter('form[name=entry]')->form();
1083 $data = [
1084 'entry[url]' => $this->url,
1085 ];
1086 $client->submit($form, $data);
1087 $content = $client->getContainer()
1088 ->get('doctrine.orm.entity_manager')
1089 ->getRepository('WallabagCoreBundle:Entry')
1090 ->findByUrlAndUserId($this->url, $this->getLoggedInUserId());
1091 $client->request('GET', '/archive/'.$content->getId());
1092
1093 $crawler = $client->request('GET', '/archive/list'); 1198 $crawler = $client->request('GET', '/archive/list');
1094 1199
1200 // Added new article to test on archive list
1201 $entry = new Entry($this->getLoggedInUser());
1202 $entry->setUrl('http://0.0.0.0/foo/baz/qux');
1203 $entry->setTitle('Le manège');
1204 $entry->setArchived(true);
1205 $this->getEntityManager()->persist($entry);
1206 $this->getEntityManager()->flush();
1207
1095 $form = $crawler->filter('form[name=search]')->form(); 1208 $form = $crawler->filter('form[name=search]')->form();
1096 $data = [ 1209 $data = [
1097 'search_entry[term]' => 'manège', 1210 'search_entry[term]' => 'manège',
@@ -1100,7 +1213,7 @@ class EntryControllerTest extends WallabagCoreTestCase
1100 $crawler = $client->submit($form, $data); 1213 $crawler = $client->submit($form, $data);
1101 1214
1102 $this->assertCount(1, $crawler->filter('div[class=entry]')); 1215 $this->assertCount(1, $crawler->filter('div[class=entry]'));
1103 $client->request('GET', '/delete/'.$content->getId()); 1216 $client->request('GET', '/delete/'.$entry->getId());
1104 1217
1105 // test on list of all articles 1218 // test on list of all articles
1106 $crawler = $client->request('GET', '/all/list'); 1219 $crawler = $client->request('GET', '/all/list');
@@ -1115,6 +1228,13 @@ class EntryControllerTest extends WallabagCoreTestCase
1115 $this->assertCount(0, $crawler->filter('div[class=entry]')); 1228 $this->assertCount(0, $crawler->filter('div[class=entry]'));
1116 1229
1117 // test url search on list of all articles 1230 // test url search on list of all articles
1231 $entry = new Entry($this->getLoggedInUser());
1232 $entry->setUrl('http://domain/qux');
1233 $entry->setTitle('Le manège');
1234 $entry->setArchived(true);
1235 $this->getEntityManager()->persist($entry);
1236 $this->getEntityManager()->flush();
1237
1118 $crawler = $client->request('GET', '/all/list'); 1238 $crawler = $client->request('GET', '/all/list');
1119 1239
1120 $form = $crawler->filter('form[name=search]')->form(); 1240 $form = $crawler->filter('form[name=search]')->form();
@@ -1138,4 +1258,134 @@ class EntryControllerTest extends WallabagCoreTestCase
1138 1258
1139 $this->assertCount(1, $crawler->filter('div[class=entry]')); 1259 $this->assertCount(1, $crawler->filter('div[class=entry]'));
1140 } 1260 }
1261
1262 public function dataForLanguage()
1263 {
1264 return [
1265 'ru' => [
1266 'https://www.pravda.ru/world/09-06-2017/1337283-qatar-0/',
1267 'ru',
1268 ],
1269 'fr-FR' => [
1270 'http://www.zataz.com/90-des-dossiers-medicaux-des-coreens-du-sud-vendus-a-des-entreprises-privees/',
1271 'fr_FR',
1272 ],
1273 'de' => [
1274 'http://www.bild.de/politik/ausland/theresa-may/wahlbeben-grossbritannien-analyse-52108924.bild.html',
1275 'de',
1276 ],
1277 'it' => [
1278 'http://www.ansa.it/sito/notizie/mondo/europa/2017/06/08/voto-gb-seggi-aperti-misure-sicurezza-rafforzate_0cb71f7f-e23b-4d5f-95ca-bc12296419f0.html',
1279 'it',
1280 ],
1281 'zh_CN' => [
1282 'http://www.hao123.com/shequ?__noscript__-=1',
1283 'zh_CN',
1284 ],
1285 'de_AT' => [
1286 'https://buy.garmin.com/de-AT/AT/catalog/product/compareResult.ep?compareProduct=112885&compareProduct=36728',
1287 'de_AT',
1288 ],
1289 'ru_RU' => [
1290 'http://netler.ru/ikt/windows-error-reporting.htm',
1291 'ru_RU',
1292 ],
1293 'pt_BR' => [
1294 'http://precodoscombustiveis.com.br/postos/cidade/4121/pr/maringa',
1295 'pt_BR',
1296 ],
1297 'fucked_list_of_languages' => [
1298 'http://geocatalog.webservice-energy.org/geonetwork/srv/eng/main.home',
1299 '',
1300 ],
1301 'es-ES' => [
1302 'http://www.muylinux.com/2015/04/17/odf-reino-unido-microsoft-google',
1303 'es_ES',
1304 ],
1305 ];
1306 }
1307
1308 /**
1309 * @dataProvider dataForLanguage
1310 */
1311 public function testLanguageValidation($url, $expectedLanguage)
1312 {
1313 $this->logInAs('admin');
1314 $client = $this->getClient();
1315
1316 $crawler = $client->request('GET', '/new');
1317
1318 $this->assertEquals(200, $client->getResponse()->getStatusCode());
1319
1320 $form = $crawler->filter('form[name=entry]')->form();
1321
1322 $data = [
1323 'entry[url]' => $url,
1324 ];
1325
1326 $client->submit($form, $data);
1327
1328 $this->assertEquals(302, $client->getResponse()->getStatusCode());
1329
1330 $content = $client->getContainer()
1331 ->get('doctrine.orm.entity_manager')
1332 ->getRepository('WallabagCoreBundle:Entry')
1333 ->findByUrlAndUserId($url, $this->getLoggedInUserId());
1334
1335 $this->assertInstanceOf('Wallabag\CoreBundle\Entity\Entry', $content);
1336 $this->assertEquals($url, $content->getUrl());
1337 $this->assertEquals($expectedLanguage, $content->getLanguage());
1338 }
1339
1340 /**
1341 * This test will require an internet connection.
1342 */
1343 public function testRestrictedArticle()
1344 {
1345 $url = 'http://www.monde-diplomatique.fr/2017/05/BONNET/57475';
1346 $this->logInAs('admin');
1347 $client = $this->getClient();
1348 $em = $client->getContainer()->get('doctrine.orm.entity_manager');
1349
1350 // enable restricted access
1351 $client->getContainer()->get('craue_config')->set('restricted_access', 1);
1352
1353 // create a new site_credential
1354 $user = $client->getContainer()->get('security.token_storage')->getToken()->getUser();
1355 $credential = new SiteCredential($user);
1356 $credential->setHost('monde-diplomatique.fr');
1357 $credential->setUsername($client->getContainer()->get('wallabag_core.helper.crypto_proxy')->crypt('foo'));
1358 $credential->setPassword($client->getContainer()->get('wallabag_core.helper.crypto_proxy')->crypt('bar'));
1359
1360 $em->persist($credential);
1361 $em->flush();
1362
1363 $crawler = $client->request('GET', '/new');
1364
1365 $this->assertEquals(200, $client->getResponse()->getStatusCode());
1366
1367 $form = $crawler->filter('form[name=entry]')->form();
1368
1369 $data = [
1370 'entry[url]' => $url,
1371 ];
1372
1373 $client->submit($form, $data);
1374
1375 $this->assertEquals(302, $client->getResponse()->getStatusCode());
1376
1377 $crawler = $client->followRedirect();
1378
1379 $this->assertEquals(200, $client->getResponse()->getStatusCode());
1380 $this->assertContains('flashes.entry.notice.entry_saved', $crawler->filter('body')->extract(['_text'])[0]);
1381
1382 $content = $em
1383 ->getRepository('WallabagCoreBundle:Entry')
1384 ->findByUrlAndUserId($url, $this->getLoggedInUserId());
1385
1386 $this->assertInstanceOf('Wallabag\CoreBundle\Entity\Entry', $content);
1387 $this->assertSame('Crimes et réformes aux Philippines', $content->getTitle());
1388
1389 $client->getContainer()->get('craue_config')->set('restricted_access', 0);
1390 }
1141} 1391}
diff --git a/tests/Wallabag/CoreBundle/Controller/ExportControllerTest.php b/tests/Wallabag/CoreBundle/Controller/ExportControllerTest.php
index 32a18e26..b38961d3 100644
--- a/tests/Wallabag/CoreBundle/Controller/ExportControllerTest.php
+++ b/tests/Wallabag/CoreBundle/Controller/ExportControllerTest.php
@@ -189,11 +189,9 @@ class ExportControllerTest extends WallabagCoreTestCase
189 $this->assertContains($contentInDB[0]['language'], $csv[1]); 189 $this->assertContains($contentInDB[0]['language'], $csv[1]);
190 $this->assertContains($contentInDB[0]['createdAt']->format('d/m/Y h:i:s'), $csv[1]); 190 $this->assertContains($contentInDB[0]['createdAt']->format('d/m/Y h:i:s'), $csv[1]);
191 191
192 $expectedTag = [];
193 foreach ($contentInDB[0]['tags'] as $tag) { 192 foreach ($contentInDB[0]['tags'] as $tag) {
194 $expectedTag[] = $tag['label']; 193 $this->assertContains($tag['label'], $csv[1]);
195 } 194 }
196 $this->assertContains(implode(', ', $expectedTag), $csv[1]);
197 } 195 }
198 196
199 public function testJsonExport() 197 public function testJsonExport()
diff --git a/tests/Wallabag/CoreBundle/Controller/RssControllerTest.php b/tests/Wallabag/CoreBundle/Controller/RssControllerTest.php
index 5a59654d..530c8bbf 100644
--- a/tests/Wallabag/CoreBundle/Controller/RssControllerTest.php
+++ b/tests/Wallabag/CoreBundle/Controller/RssControllerTest.php
@@ -6,7 +6,7 @@ use Tests\Wallabag\CoreBundle\WallabagCoreTestCase;
6 6
7class RssControllerTest extends WallabagCoreTestCase 7class RssControllerTest extends WallabagCoreTestCase
8{ 8{
9 public function validateDom($xml, $type, $nb = null) 9 public function validateDom($xml, $type, $urlPagination, $nb = null)
10 { 10 {
11 $doc = new \DOMDocument(); 11 $doc = new \DOMDocument();
12 $doc->loadXML($xml); 12 $doc->loadXML($xml);
@@ -23,7 +23,7 @@ class RssControllerTest extends WallabagCoreTestCase
23 $this->assertEquals(1, $xpath->query('/rss/channel')->length); 23 $this->assertEquals(1, $xpath->query('/rss/channel')->length);
24 24
25 $this->assertEquals(1, $xpath->query('/rss/channel/title')->length); 25 $this->assertEquals(1, $xpath->query('/rss/channel/title')->length);
26 $this->assertEquals('wallabag '.$type.' feed', $xpath->query('/rss/channel/title')->item(0)->nodeValue); 26 $this->assertEquals('wallabag - '.$type.' feed', $xpath->query('/rss/channel/title')->item(0)->nodeValue);
27 27
28 $this->assertEquals(1, $xpath->query('/rss/channel/pubDate')->length); 28 $this->assertEquals(1, $xpath->query('/rss/channel/pubDate')->length);
29 29
@@ -34,10 +34,10 @@ class RssControllerTest extends WallabagCoreTestCase
34 $this->assertEquals('wallabag '.$type.' elements', $xpath->query('/rss/channel/description')->item(0)->nodeValue); 34 $this->assertEquals('wallabag '.$type.' elements', $xpath->query('/rss/channel/description')->item(0)->nodeValue);
35 35
36 $this->assertEquals(1, $xpath->query('/rss/channel/link[@rel="self"]')->length); 36 $this->assertEquals(1, $xpath->query('/rss/channel/link[@rel="self"]')->length);
37 $this->assertContains($type.'.xml', $xpath->query('/rss/channel/link[@rel="self"]')->item(0)->getAttribute('href')); 37 $this->assertContains($urlPagination.'.xml', $xpath->query('/rss/channel/link[@rel="self"]')->item(0)->getAttribute('href'));
38 38
39 $this->assertEquals(1, $xpath->query('/rss/channel/link[@rel="last"]')->length); 39 $this->assertEquals(1, $xpath->query('/rss/channel/link[@rel="last"]')->length);
40 $this->assertContains($type.'.xml?page=', $xpath->query('/rss/channel/link[@rel="last"]')->item(0)->getAttribute('href')); 40 $this->assertContains($urlPagination.'.xml?page=', $xpath->query('/rss/channel/link[@rel="last"]')->item(0)->getAttribute('href'));
41 41
42 foreach ($xpath->query('//item') as $item) { 42 foreach ($xpath->query('//item') as $item) {
43 $this->assertEquals(1, $xpath->query('title', $item)->length); 43 $this->assertEquals(1, $xpath->query('title', $item)->length);
@@ -94,7 +94,7 @@ class RssControllerTest extends WallabagCoreTestCase
94 94
95 $this->assertEquals(200, $client->getResponse()->getStatusCode()); 95 $this->assertEquals(200, $client->getResponse()->getStatusCode());
96 96
97 $this->validateDom($client->getResponse()->getContent(), 'unread', 2); 97 $this->validateDom($client->getResponse()->getContent(), 'unread', 'unread', 2);
98 } 98 }
99 99
100 public function testStarred() 100 public function testStarred()
@@ -116,7 +116,7 @@ class RssControllerTest extends WallabagCoreTestCase
116 116
117 $this->assertEquals(200, $client->getResponse()->getStatusCode(), 1); 117 $this->assertEquals(200, $client->getResponse()->getStatusCode(), 1);
118 118
119 $this->validateDom($client->getResponse()->getContent(), 'starred'); 119 $this->validateDom($client->getResponse()->getContent(), 'starred', 'starred');
120 } 120 }
121 121
122 public function testArchives() 122 public function testArchives()
@@ -138,7 +138,7 @@ class RssControllerTest extends WallabagCoreTestCase
138 138
139 $this->assertEquals(200, $client->getResponse()->getStatusCode()); 139 $this->assertEquals(200, $client->getResponse()->getStatusCode());
140 140
141 $this->validateDom($client->getResponse()->getContent(), 'archive'); 141 $this->validateDom($client->getResponse()->getContent(), 'archive', 'archive');
142 } 142 }
143 143
144 public function testPagination() 144 public function testPagination()
@@ -159,13 +159,38 @@ class RssControllerTest extends WallabagCoreTestCase
159 159
160 $client->request('GET', '/admin/SUPERTOKEN/unread.xml'); 160 $client->request('GET', '/admin/SUPERTOKEN/unread.xml');
161 $this->assertEquals(200, $client->getResponse()->getStatusCode()); 161 $this->assertEquals(200, $client->getResponse()->getStatusCode());
162 $this->validateDom($client->getResponse()->getContent(), 'unread'); 162 $this->validateDom($client->getResponse()->getContent(), 'unread', 'unread');
163 163
164 $client->request('GET', '/admin/SUPERTOKEN/unread.xml?page=2'); 164 $client->request('GET', '/admin/SUPERTOKEN/unread.xml?page=2');
165 $this->assertEquals(200, $client->getResponse()->getStatusCode()); 165 $this->assertEquals(200, $client->getResponse()->getStatusCode());
166 $this->validateDom($client->getResponse()->getContent(), 'unread'); 166 $this->validateDom($client->getResponse()->getContent(), 'unread', 'unread');
167 167
168 $client->request('GET', '/admin/SUPERTOKEN/unread.xml?page=3000'); 168 $client->request('GET', '/admin/SUPERTOKEN/unread.xml?page=3000');
169 $this->assertEquals(302, $client->getResponse()->getStatusCode()); 169 $this->assertEquals(302, $client->getResponse()->getStatusCode());
170 } 170 }
171
172 public function testTags()
173 {
174 $client = $this->getClient();
175 $em = $client->getContainer()->get('doctrine.orm.entity_manager');
176 $user = $em
177 ->getRepository('WallabagUserBundle:User')
178 ->findOneByUsername('admin');
179
180 $config = $user->getConfig();
181 $config->setRssToken('SUPERTOKEN');
182 $config->setRssLimit(null);
183 $em->persist($config);
184 $em->flush();
185
186 $client = $this->getClient();
187 $client->request('GET', '/admin/SUPERTOKEN/tags/foo-bar.xml');
188
189 $this->assertEquals(200, $client->getResponse()->getStatusCode());
190
191 $this->validateDom($client->getResponse()->getContent(), 'tag (foo bar)', 'tags/foo-bar');
192
193 $client->request('GET', '/admin/SUPERTOKEN/tags/foo-bar.xml?page=3000');
194 $this->assertEquals(302, $client->getResponse()->getStatusCode());
195 }
171} 196}
diff --git a/tests/Wallabag/CoreBundle/Controller/SiteCredentialControllerTest.php b/tests/Wallabag/CoreBundle/Controller/SiteCredentialControllerTest.php
new file mode 100644
index 00000000..e73a9743
--- /dev/null
+++ b/tests/Wallabag/CoreBundle/Controller/SiteCredentialControllerTest.php
@@ -0,0 +1,139 @@
1<?php
2
3namespace Tests\Wallabag\CoreBundle\Controller;
4
5use Symfony\Bundle\FrameworkBundle\Client;
6use Tests\Wallabag\CoreBundle\WallabagCoreTestCase;
7use Wallabag\CoreBundle\Entity\SiteCredential;
8
9class SiteCredentialControllerTest extends WallabagCoreTestCase
10{
11 public function testListSiteCredential()
12 {
13 $this->logInAs('admin');
14 $client = $this->getClient();
15
16 $crawler = $client->request('GET', '/site-credentials/');
17
18 $this->assertEquals(200, $client->getResponse()->getStatusCode());
19
20 $body = $crawler->filter('body')->extract(['_text'])[0];
21
22 $this->assertContains('site_credential.description', $body);
23 $this->assertContains('site_credential.list.create_new_one', $body);
24 }
25
26 public function testNewSiteCredential()
27 {
28 $this->logInAs('admin');
29 $client = $this->getClient();
30
31 $crawler = $client->request('GET', '/site-credentials/new');
32
33 $this->assertEquals(200, $client->getResponse()->getStatusCode());
34
35 $body = $crawler->filter('body')->extract(['_text'])[0];
36
37 $this->assertContains('site_credential.new_site_credential', $body);
38 $this->assertContains('site_credential.form.back_to_list', $body);
39
40 $form = $crawler->filter('button[id=site_credential_save]')->form();
41
42 $data = [
43 'site_credential[host]' => 'google.io',
44 'site_credential[username]' => 'sergei',
45 'site_credential[password]' => 'microsoft',
46 ];
47
48 $client->submit($form, $data);
49
50 $this->assertEquals(302, $client->getResponse()->getStatusCode());
51
52 $crawler = $client->followRedirect();
53
54 $this->assertContains('flashes.site_credential.notice.added', $crawler->filter('body')->extract(['_text'])[0]);
55 }
56
57 public function testEditSiteCredential()
58 {
59 $this->logInAs('admin');
60 $client = $this->getClient();
61
62 $credential = $this->createSiteCredential($client);
63
64 $crawler = $client->request('GET', '/site-credentials/'.$credential->getId().'/edit');
65
66 $this->assertEquals(200, $client->getResponse()->getStatusCode());
67
68 $body = $crawler->filter('body')->extract(['_text'])[0];
69
70 $this->assertContains('site_credential.edit_site_credential', $body);
71 $this->assertContains('site_credential.form.back_to_list', $body);
72
73 $form = $crawler->filter('button[id=site_credential_save]')->form();
74
75 $data = [
76 'site_credential[host]' => 'google.io',
77 'site_credential[username]' => 'larry',
78 'site_credential[password]' => 'microsoft',
79 ];
80
81 $client->submit($form, $data);
82
83 $this->assertEquals(302, $client->getResponse()->getStatusCode());
84
85 $crawler = $client->followRedirect();
86
87 $this->assertContains('flashes.site_credential.notice.updated', $crawler->filter('body')->extract(['_text'])[0]);
88 }
89
90 public function testEditFromADifferentUserSiteCredential()
91 {
92 $this->logInAs('admin');
93 $client = $this->getClient();
94
95 $credential = $this->createSiteCredential($client);
96
97 $this->logInAs('bob');
98
99 $client->request('GET', '/site-credentials/'.$credential->getId().'/edit');
100
101 $this->assertEquals(403, $client->getResponse()->getStatusCode());
102 }
103
104 public function testDeleteSiteCredential()
105 {
106 $this->logInAs('admin');
107 $client = $this->getClient();
108
109 $credential = $this->createSiteCredential($client);
110
111 $crawler = $client->request('GET', '/site-credentials/'.$credential->getId().'/edit');
112
113 $this->assertEquals(200, $client->getResponse()->getStatusCode());
114
115 $deleteForm = $crawler->filter('body')->selectButton('site_credential.form.delete')->form();
116
117 $client->submit($deleteForm, []);
118
119 $this->assertEquals(302, $client->getResponse()->getStatusCode());
120
121 $crawler = $client->followRedirect();
122
123 $this->assertContains('flashes.site_credential.notice.deleted', $crawler->filter('body')->extract(['_text'])[0]);
124 }
125
126 private function createSiteCredential(Client $client)
127 {
128 $credential = new SiteCredential($this->getLoggedInUser());
129 $credential->setHost('google.io');
130 $credential->setUsername('sergei');
131 $credential->setPassword('microsoft');
132
133 $em = $client->getContainer()->get('doctrine.orm.entity_manager');
134 $em->persist($credential);
135 $em->flush();
136
137 return $credential;
138 }
139}
diff --git a/tests/Wallabag/CoreBundle/Controller/TagControllerTest.php b/tests/Wallabag/CoreBundle/Controller/TagControllerTest.php
index fa1a3539..af1ad7af 100644
--- a/tests/Wallabag/CoreBundle/Controller/TagControllerTest.php
+++ b/tests/Wallabag/CoreBundle/Controller/TagControllerTest.php
@@ -3,6 +3,7 @@
3namespace Tests\Wallabag\CoreBundle\Controller; 3namespace Tests\Wallabag\CoreBundle\Controller;
4 4
5use Tests\Wallabag\CoreBundle\WallabagCoreTestCase; 5use Tests\Wallabag\CoreBundle\WallabagCoreTestCase;
6use Wallabag\CoreBundle\Entity\Entry;
6use Wallabag\CoreBundle\Entity\Tag; 7use Wallabag\CoreBundle\Entity\Tag;
7 8
8class TagControllerTest extends WallabagCoreTestCase 9class TagControllerTest extends WallabagCoreTestCase
@@ -24,10 +25,11 @@ class TagControllerTest extends WallabagCoreTestCase
24 $this->logInAs('admin'); 25 $this->logInAs('admin');
25 $client = $this->getClient(); 26 $client = $this->getClient();
26 27
27 $entry = $client->getContainer() 28 $entry = new Entry($this->getLoggedInUser());
28 ->get('doctrine.orm.entity_manager') 29 $entry->setUrl('http://0.0.0.0/foo');
29 ->getRepository('WallabagCoreBundle:Entry') 30 $this->getEntityManager()->persist($entry);
30 ->findByUrlAndUserId('http://0.0.0.0/entry1', $this->getLoggedInUserId()); 31 $this->getEntityManager()->flush();
32 $this->getEntityManager()->clear();
31 33
32 $crawler = $client->request('GET', '/view/'.$entry->getId()); 34 $crawler = $client->request('GET', '/view/'.$entry->getId());
33 35
@@ -41,23 +43,15 @@ class TagControllerTest extends WallabagCoreTestCase
41 $this->assertEquals(302, $client->getResponse()->getStatusCode()); 43 $this->assertEquals(302, $client->getResponse()->getStatusCode());
42 44
43 // be sure to reload the entry 45 // be sure to reload the entry
44 $entry = $client->getContainer() 46 $entry = $this->getEntityManager()->getRepository(Entry::class)->find($entry->getId());
45 ->get('doctrine.orm.entity_manager') 47 $this->assertCount(1, $entry->getTags());
46 ->getRepository('WallabagCoreBundle:Entry')
47 ->findByUrlAndUserId('http://0.0.0.0/entry1', $this->getLoggedInUserId());
48
49 $this->assertEquals(3, count($entry->getTags()));
50 48
51 // tag already exists and already assigned 49 // tag already exists and already assigned
52 $client->submit($form, $data); 50 $client->submit($form, $data);
53 $this->assertEquals(302, $client->getResponse()->getStatusCode()); 51 $this->assertEquals(302, $client->getResponse()->getStatusCode());
54 52
55 $newEntry = $client->getContainer() 53 $entry = $this->getEntityManager()->getRepository(Entry::class)->find($entry->getId());
56 ->get('doctrine.orm.entity_manager') 54 $this->assertCount(1, $entry->getTags());
57 ->getRepository('WallabagCoreBundle:Entry')
58 ->find($entry->getId());
59
60 $this->assertEquals(3, count($newEntry->getTags()));
61 55
62 // tag already exists but still not assigned to this entry 56 // tag already exists but still not assigned to this entry
63 $data = [ 57 $data = [
@@ -67,12 +61,8 @@ class TagControllerTest extends WallabagCoreTestCase
67 $client->submit($form, $data); 61 $client->submit($form, $data);
68 $this->assertEquals(302, $client->getResponse()->getStatusCode()); 62 $this->assertEquals(302, $client->getResponse()->getStatusCode());
69 63
70 $newEntry = $client->getContainer() 64 $entry = $this->getEntityManager()->getRepository(Entry::class)->find($entry->getId());
71 ->get('doctrine.orm.entity_manager') 65 $this->assertCount(2, $entry->getTags());
72 ->getRepository('WallabagCoreBundle:Entry')
73 ->find($entry->getId());
74
75 $this->assertEquals(3, count($newEntry->getTags()));
76 } 66 }
77 67
78 public function testAddMultipleTagToEntry() 68 public function testAddMultipleTagToEntry()
@@ -116,20 +106,25 @@ class TagControllerTest extends WallabagCoreTestCase
116 $this->logInAs('admin'); 106 $this->logInAs('admin');
117 $client = $this->getClient(); 107 $client = $this->getClient();
118 108
119 $entry = $client->getContainer() 109 $tag = new Tag();
120 ->get('doctrine.orm.entity_manager') 110 $tag->setLabel($this->tagName);
121 ->getRepository('WallabagCoreBundle:Entry') 111 $entry = new Entry($this->getLoggedInUser());
122 ->findByUrlAndUserId('http://0.0.0.0/entry1', $this->getLoggedInUserId()); 112 $entry->setUrl('http://0.0.0.0/foo');
123 113 $entry->addTag($tag);
124 $tag = $client->getContainer() 114 $this->getEntityManager()->persist($entry);
125 ->get('doctrine.orm.entity_manager') 115 $this->getEntityManager()->flush();
126 ->getRepository('WallabagCoreBundle:Tag') 116 $this->getEntityManager()->clear();
127 ->findOneByEntryAndTagLabel($entry, $this->tagName); 117
128 118 // We make a first request to set an history and test redirection after tag deletion
119 $client->request('GET', '/view/'.$entry->getId());
120 $entryUri = $client->getRequest()->getUri();
129 $client->request('GET', '/remove-tag/'.$entry->getId().'/'.$tag->getId()); 121 $client->request('GET', '/remove-tag/'.$entry->getId().'/'.$tag->getId());
130 122
131 $this->assertEquals(302, $client->getResponse()->getStatusCode()); 123 $this->assertEquals(302, $client->getResponse()->getStatusCode());
124 $this->assertEquals($entryUri, $client->getResponse()->getTargetUrl());
132 125
126 // re-retrieve the entry to be sure to get fresh data from database (mostly for tags)
127 $entry = $this->getEntityManager()->getRepository(Entry::class)->find($entry->getId());
133 $this->assertNotContains($this->tagName, $entry->getTags()); 128 $this->assertNotContains($this->tagName, $entry->getTags());
134 129
135 $client->request('GET', '/remove-tag/'.$entry->getId().'/'.$tag->getId()); 130 $client->request('GET', '/remove-tag/'.$entry->getId().'/'.$tag->getId());
diff --git a/tests/Wallabag/CoreBundle/GuzzleSiteAuthenticator/GrabySiteConfigBuilderTest.php b/tests/Wallabag/CoreBundle/GuzzleSiteAuthenticator/GrabySiteConfigBuilderTest.php
index aee67259..b0c81e7b 100644
--- a/tests/Wallabag/CoreBundle/GuzzleSiteAuthenticator/GrabySiteConfigBuilderTest.php
+++ b/tests/Wallabag/CoreBundle/GuzzleSiteAuthenticator/GrabySiteConfigBuilderTest.php
@@ -2,12 +2,15 @@
2 2
3namespace Tests\Wallabag\CoreBundle\GuzzleSiteAuthenticator; 3namespace Tests\Wallabag\CoreBundle\GuzzleSiteAuthenticator;
4 4
5use Monolog\Handler\TestHandler;
6use Monolog\Logger;
5use BD\GuzzleSiteAuthenticator\SiteConfig\SiteConfig; 7use BD\GuzzleSiteAuthenticator\SiteConfig\SiteConfig;
6use Graby\SiteConfig\SiteConfig as GrabySiteConfig; 8use Graby\SiteConfig\SiteConfig as GrabySiteConfig;
7use PHPUnit_Framework_TestCase;
8use Wallabag\CoreBundle\GuzzleSiteAuthenticator\GrabySiteConfigBuilder; 9use Wallabag\CoreBundle\GuzzleSiteAuthenticator\GrabySiteConfigBuilder;
10use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
11use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage;
9 12
10class GrabySiteConfigBuilderTest extends PHPUnit_Framework_TestCase 13class GrabySiteConfigBuilderTest extends \PHPUnit_Framework_TestCase
11{ 14{
12 /** @var \Wallabag\CoreBundle\GuzzleSiteAuthenticator\GrabySiteConfigBuilder */ 15 /** @var \Wallabag\CoreBundle\GuzzleSiteAuthenticator\GrabySiteConfigBuilder */
13 protected $builder; 16 protected $builder;
@@ -15,16 +18,16 @@ class GrabySiteConfigBuilderTest extends PHPUnit_Framework_TestCase
15 public function testBuildConfigExists() 18 public function testBuildConfigExists()
16 { 19 {
17 /* @var \Graby\SiteConfig\ConfigBuilder|\PHPUnit_Framework_MockObject_MockObject */ 20 /* @var \Graby\SiteConfig\ConfigBuilder|\PHPUnit_Framework_MockObject_MockObject */
18 $grabyConfigBuilderMock = $this->getMockBuilder('\Graby\SiteConfig\ConfigBuilder') 21 $grabyConfigBuilderMock = $this->getMockBuilder('Graby\SiteConfig\ConfigBuilder')
19 ->disableOriginalConstructor() 22 ->disableOriginalConstructor()
20 ->getMock(); 23 ->getMock();
21 24
22 $grabySiteConfig = new GrabySiteConfig(); 25 $grabySiteConfig = new GrabySiteConfig();
23 $grabySiteConfig->requires_login = true; 26 $grabySiteConfig->requires_login = true;
24 $grabySiteConfig->login_uri = 'http://example.com/login'; 27 $grabySiteConfig->login_uri = 'http://www.example.com/login';
25 $grabySiteConfig->login_username_field = 'login'; 28 $grabySiteConfig->login_username_field = 'login';
26 $grabySiteConfig->login_password_field = 'password'; 29 $grabySiteConfig->login_password_field = 'password';
27 $grabySiteConfig->login_extra_fields = ['field' => 'value']; 30 $grabySiteConfig->login_extra_fields = ['field=value'];
28 $grabySiteConfig->not_logged_in_xpath = '//div[@class="need-login"]'; 31 $grabySiteConfig->not_logged_in_xpath = '//div[@class="need-login"]';
29 32
30 $grabyConfigBuilderMock 33 $grabyConfigBuilderMock
@@ -32,18 +35,44 @@ class GrabySiteConfigBuilderTest extends PHPUnit_Framework_TestCase
32 ->with('example.com') 35 ->with('example.com')
33 ->will($this->returnValue($grabySiteConfig)); 36 ->will($this->returnValue($grabySiteConfig));
34 37
38 $logger = new Logger('foo');
39 $handler = new TestHandler();
40 $logger->pushHandler($handler);
41
42 $siteCrentialRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\SiteCredentialRepository')
43 ->disableOriginalConstructor()
44 ->getMock();
45 $siteCrentialRepo->expects($this->once())
46 ->method('findOneByHostAndUser')
47 ->with('example.com', 1)
48 ->willReturn(['username' => 'foo', 'password' => 'bar']);
49
50 $user = $this->getMockBuilder('Wallabag\UserBundle\Entity\User')
51 ->disableOriginalConstructor()
52 ->getMock();
53 $user->expects($this->once())
54 ->method('getId')
55 ->willReturn(1);
56
57 $token = new UsernamePasswordToken($user, 'pass', 'provider');
58
59 $tokenStorage = new TokenStorage();
60 $tokenStorage->setToken($token);
61
35 $this->builder = new GrabySiteConfigBuilder( 62 $this->builder = new GrabySiteConfigBuilder(
36 $grabyConfigBuilderMock, 63 $grabyConfigBuilderMock,
37 ['example.com' => ['username' => 'foo', 'password' => 'bar']] 64 $tokenStorage,
65 $siteCrentialRepo,
66 $logger
38 ); 67 );
39 68
40 $config = $this->builder->buildForHost('example.com'); 69 $config = $this->builder->buildForHost('www.example.com');
41 70
42 self::assertEquals( 71 $this->assertEquals(
43 new SiteConfig([ 72 new SiteConfig([
44 'host' => 'example.com', 73 'host' => 'example.com',
45 'requiresLogin' => true, 74 'requiresLogin' => true,
46 'loginUri' => 'http://example.com/login', 75 'loginUri' => 'http://www.example.com/login',
47 'usernameField' => 'login', 76 'usernameField' => 'login',
48 'passwordField' => 'password', 77 'passwordField' => 'password',
49 'extraFields' => ['field' => 'value'], 78 'extraFields' => ['field' => 'value'],
@@ -53,6 +82,10 @@ class GrabySiteConfigBuilderTest extends PHPUnit_Framework_TestCase
53 ]), 82 ]),
54 $config 83 $config
55 ); 84 );
85
86 $records = $handler->getRecords();
87
88 $this->assertCount(1, $records, 'One log was recorded');
56 } 89 }
57 90
58 public function testBuildConfigDoesntExist() 91 public function testBuildConfigDoesntExist()
@@ -67,19 +100,43 @@ class GrabySiteConfigBuilderTest extends PHPUnit_Framework_TestCase
67 ->with('unknown.com') 100 ->with('unknown.com')
68 ->will($this->returnValue(new GrabySiteConfig())); 101 ->will($this->returnValue(new GrabySiteConfig()));
69 102
70 $this->builder = new GrabySiteConfigBuilder($grabyConfigBuilderMock, []); 103 $logger = new Logger('foo');
104 $handler = new TestHandler();
105 $logger->pushHandler($handler);
71 106
72 $config = $this->builder->buildForHost('unknown.com'); 107 $siteCrentialRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\SiteCredentialRepository')
108 ->disableOriginalConstructor()
109 ->getMock();
110 $siteCrentialRepo->expects($this->once())
111 ->method('findOneByHostAndUser')
112 ->with('unknown.com', 1)
113 ->willReturn(null);
73 114
74 self::assertEquals( 115 $user = $this->getMockBuilder('Wallabag\UserBundle\Entity\User')
75 new SiteConfig([ 116 ->disableOriginalConstructor()
76 'host' => 'unknown.com', 117 ->getMock();
77 'requiresLogin' => false, 118 $user->expects($this->once())
78 'username' => null, 119 ->method('getId')
79 'password' => null, 120 ->willReturn(1);
80 'extraFields' => [], 121
81 ]), 122 $token = new UsernamePasswordToken($user, 'pass', 'provider');
82 $config 123
124 $tokenStorage = new TokenStorage();
125 $tokenStorage->setToken($token);
126
127 $this->builder = new GrabySiteConfigBuilder(
128 $grabyConfigBuilderMock,
129 $tokenStorage,
130 $siteCrentialRepo,
131 $logger
83 ); 132 );
133
134 $config = $this->builder->buildForHost('unknown.com');
135
136 $this->assertFalse($config);
137
138 $records = $handler->getRecords();
139
140 $this->assertCount(1, $records, 'One log was recorded');
84 } 141 }
85} 142}
diff --git a/tests/Wallabag/CoreBundle/Helper/ContentProxyTest.php b/tests/Wallabag/CoreBundle/Helper/ContentProxyTest.php
index 4f70ed0c..dbddbc5c 100644
--- a/tests/Wallabag/CoreBundle/Helper/ContentProxyTest.php
+++ b/tests/Wallabag/CoreBundle/Helper/ContentProxyTest.php
@@ -3,10 +3,17 @@
3namespace Tests\Wallabag\CoreBundle\Helper; 3namespace Tests\Wallabag\CoreBundle\Helper;
4 4
5use Psr\Log\NullLogger; 5use Psr\Log\NullLogger;
6use Monolog\Logger;
7use Monolog\Handler\TestHandler;
6use Wallabag\CoreBundle\Helper\ContentProxy; 8use Wallabag\CoreBundle\Helper\ContentProxy;
7use Wallabag\CoreBundle\Entity\Entry; 9use Wallabag\CoreBundle\Entity\Entry;
8use Wallabag\CoreBundle\Entity\Tag; 10use Wallabag\CoreBundle\Entity\Tag;
9use Wallabag\UserBundle\Entity\User; 11use Wallabag\UserBundle\Entity\User;
12use Wallabag\CoreBundle\Helper\RuleBasedTagger;
13use Graby\Graby;
14use Symfony\Component\Validator\Validator\RecursiveValidator;
15use Symfony\Component\Validator\ConstraintViolationList;
16use Symfony\Component\Validator\ConstraintViolation;
10 17
11class ContentProxyTest extends \PHPUnit_Framework_TestCase 18class ContentProxyTest extends \PHPUnit_Framework_TestCase
12{ 19{
@@ -33,8 +40,9 @@ class ContentProxyTest extends \PHPUnit_Framework_TestCase
33 'language' => '', 40 'language' => '',
34 ]); 41 ]);
35 42
36 $proxy = new ContentProxy($graby, $tagger, $this->getTagRepositoryMock(), $this->getLogger(), $this->fetchingErrorMessage); 43 $proxy = new ContentProxy($graby, $tagger, $this->getValidator(), $this->getLogger(), $this->fetchingErrorMessage);
37 $entry = $proxy->updateEntry(new Entry(new User()), 'http://user@:80'); 44 $entry = new Entry(new User());
45 $proxy->updateEntry($entry, 'http://user@:80');
38 46
39 $this->assertEquals('http://user@:80', $entry->getUrl()); 47 $this->assertEquals('http://user@:80', $entry->getUrl());
40 $this->assertEmpty($entry->getTitle()); 48 $this->assertEmpty($entry->getTitle());
@@ -67,8 +75,9 @@ class ContentProxyTest extends \PHPUnit_Framework_TestCase
67 'language' => '', 75 'language' => '',
68 ]); 76 ]);
69 77
70 $proxy = new ContentProxy($graby, $tagger, $this->getTagRepositoryMock(), $this->getLogger(), $this->fetchingErrorMessage); 78 $proxy = new ContentProxy($graby, $tagger, $this->getValidator(), $this->getLogger(), $this->fetchingErrorMessage);
71 $entry = $proxy->updateEntry(new Entry(new User()), 'http://0.0.0.0'); 79 $entry = new Entry(new User());
80 $proxy->updateEntry($entry, 'http://0.0.0.0');
72 81
73 $this->assertEquals('http://0.0.0.0', $entry->getUrl()); 82 $this->assertEquals('http://0.0.0.0', $entry->getUrl());
74 $this->assertEmpty($entry->getTitle()); 83 $this->assertEmpty($entry->getTitle());
@@ -106,12 +115,13 @@ class ContentProxyTest extends \PHPUnit_Framework_TestCase
106 ], 115 ],
107 ]); 116 ]);
108 117
109 $proxy = new ContentProxy($graby, $tagger, $this->getTagRepositoryMock(), $this->getLogger(), $this->fetchingErrorMessage); 118 $proxy = new ContentProxy($graby, $tagger, $this->getValidator(), $this->getLogger(), $this->fetchingErrorMessage);
110 $entry = $proxy->updateEntry(new Entry(new User()), 'http://domain.io'); 119 $entry = new Entry(new User());
120 $proxy->updateEntry($entry, 'http://domain.io');
111 121
112 $this->assertEquals('http://domain.io', $entry->getUrl()); 122 $this->assertEquals('http://domain.io', $entry->getUrl());
113 $this->assertEquals('my title', $entry->getTitle()); 123 $this->assertEquals('my title', $entry->getTitle());
114 $this->assertEquals($this->fetchingErrorMessage . '<p><i>But we found a short description: </i></p>desc', $entry->getContent()); 124 $this->assertEquals($this->fetchingErrorMessage.'<p><i>But we found a short description: </i></p>desc', $entry->getContent());
115 $this->assertEmpty($entry->getPreviewPicture()); 125 $this->assertEmpty($entry->getPreviewPicture());
116 $this->assertEmpty($entry->getLanguage()); 126 $this->assertEmpty($entry->getLanguage());
117 $this->assertEmpty($entry->getHttpStatus()); 127 $this->assertEmpty($entry->getHttpStatus());
@@ -147,8 +157,9 @@ class ContentProxyTest extends \PHPUnit_Framework_TestCase
147 ], 157 ],
148 ]); 158 ]);
149 159
150 $proxy = new ContentProxy($graby, $tagger, $this->getTagRepositoryMock(), $this->getLogger(), $this->fetchingErrorMessage); 160 $proxy = new ContentProxy($graby, $tagger, $this->getValidator(), $this->getLogger(), $this->fetchingErrorMessage);
151 $entry = $proxy->updateEntry(new Entry(new User()), 'http://0.0.0.0'); 161 $entry = new Entry(new User());
162 $proxy->updateEntry($entry, 'http://0.0.0.0');
152 163
153 $this->assertEquals('http://1.1.1.1', $entry->getUrl()); 164 $this->assertEquals('http://1.1.1.1', $entry->getUrl());
154 $this->assertEquals('this is my title', $entry->getTitle()); 165 $this->assertEquals('this is my title', $entry->getTitle());
@@ -184,12 +195,13 @@ class ContentProxyTest extends \PHPUnit_Framework_TestCase
184 'open_graph' => [ 195 'open_graph' => [
185 'og_title' => 'my OG title', 196 'og_title' => 'my OG title',
186 'og_description' => 'OG desc', 197 'og_description' => 'OG desc',
187 'og_image' => false, 198 'og_image' => null,
188 ], 199 ],
189 ]); 200 ]);
190 201
191 $proxy = new ContentProxy($graby, $tagger, $this->getTagRepositoryMock(), $this->getLogger(), $this->fetchingErrorMessage); 202 $proxy = new ContentProxy($graby, $tagger, $this->getValidator(), $this->getLogger(), $this->fetchingErrorMessage);
192 $entry = $proxy->updateEntry(new Entry(new User()), 'http://0.0.0.0'); 203 $entry = new Entry(new User());
204 $proxy->updateEntry($entry, 'http://0.0.0.0');
193 205
194 $this->assertEquals('http://1.1.1.1', $entry->getUrl()); 206 $this->assertEquals('http://1.1.1.1', $entry->getUrl());
195 $this->assertEquals('this is my title', $entry->getTitle()); 207 $this->assertEquals('this is my title', $entry->getTitle());
@@ -202,187 +214,308 @@ class ContentProxyTest extends \PHPUnit_Framework_TestCase
202 $this->assertEquals('1.1.1.1', $entry->getDomainName()); 214 $this->assertEquals('1.1.1.1', $entry->getDomainName());
203 } 215 }
204 216
205 public function testWithForcedContent() 217 public function testWithContentAndBadLanguage()
206 { 218 {
207 $tagger = $this->getTaggerMock(); 219 $tagger = $this->getTaggerMock();
208 $tagger->expects($this->once()) 220 $tagger->expects($this->once())
209 ->method('tag'); 221 ->method('tag');
210 222
211 $graby = $this->getMockBuilder('Graby\Graby')->getMock(); 223 $validator = $this->getValidator();
224 $validator->expects($this->exactly(2))
225 ->method('validate')
226 ->will($this->onConsecutiveCalls(
227 new ConstraintViolationList([new ConstraintViolation('oops', 'oops', [], 'oops', 'language', 'dontexist')]),
228 new ConstraintViolationList()
229 ));
212 230
213 $proxy = new ContentProxy($graby, $tagger, $this->getTagRepositoryMock(), $this->getLogger(), $this->fetchingErrorMessage); 231 $graby = $this->getMockBuilder('Graby\Graby')
214 $entry = $proxy->updateEntry(new Entry(new User()), 'http://0.0.0.0', [ 232 ->setMethods(['fetchContent'])
215 'html' => str_repeat('this is my content', 325), 233 ->disableOriginalConstructor()
216 'title' => 'this is my title', 234 ->getMock();
217 'url' => 'http://1.1.1.1', 235
218 'content_type' => 'text/html', 236 $graby->expects($this->any())
219 'language' => 'fr', 237 ->method('fetchContent')
220 ]); 238 ->willReturn([
239 'html' => str_repeat('this is my content', 325),
240 'title' => 'this is my title',
241 'url' => 'http://1.1.1.1',
242 'content_type' => 'text/html',
243 'language' => 'dontexist',
244 'status' => '200',
245 ]);
246
247 $proxy = new ContentProxy($graby, $tagger, $validator, $this->getLogger(), $this->fetchingErrorMessage);
248 $entry = new Entry(new User());
249 $proxy->updateEntry($entry, 'http://0.0.0.0');
221 250
222 $this->assertEquals('http://1.1.1.1', $entry->getUrl()); 251 $this->assertEquals('http://1.1.1.1', $entry->getUrl());
223 $this->assertEquals('this is my title', $entry->getTitle()); 252 $this->assertEquals('this is my title', $entry->getTitle());
224 $this->assertContains('this is my content', $entry->getContent()); 253 $this->assertContains('this is my content', $entry->getContent());
225 $this->assertEquals('text/html', $entry->getMimetype()); 254 $this->assertEquals('text/html', $entry->getMimetype());
226 $this->assertEquals('fr', $entry->getLanguage()); 255 $this->assertNull($entry->getLanguage());
256 $this->assertEquals('200', $entry->getHttpStatus());
227 $this->assertEquals(4.0, $entry->getReadingTime()); 257 $this->assertEquals(4.0, $entry->getReadingTime());
228 $this->assertEquals('1.1.1.1', $entry->getDomainName()); 258 $this->assertEquals('1.1.1.1', $entry->getDomainName());
229 } 259 }
230 260
231 public function testTaggerThrowException() 261 public function testWithContentAndBadOgImage()
232 { 262 {
233 $graby = $this->getMockBuilder('Graby\Graby')
234 ->disableOriginalConstructor()
235 ->getMock();
236
237 $tagger = $this->getTaggerMock(); 263 $tagger = $this->getTaggerMock();
238 $tagger->expects($this->once()) 264 $tagger->expects($this->once())
239 ->method('tag') 265 ->method('tag');
240 ->will($this->throwException(new \Exception()));
241
242 $tagRepo = $this->getTagRepositoryMock();
243 $proxy = new ContentProxy($graby, $tagger, $tagRepo, $this->getLogger(), $this->fetchingErrorMessage);
244
245 $entry = $proxy->updateEntry(new Entry(new User()), 'http://0.0.0.0', [
246 'html' => str_repeat('this is my content', 325),
247 'title' => 'this is my title',
248 'url' => 'http://1.1.1.1',
249 'content_type' => 'text/html',
250 'language' => 'fr',
251 ]);
252 266
253 $this->assertCount(0, $entry->getTags()); 267 $validator = $this->getValidator();
254 } 268 $validator->expects($this->exactly(2))
269 ->method('validate')
270 ->will($this->onConsecutiveCalls(
271 new ConstraintViolationList(),
272 new ConstraintViolationList([new ConstraintViolation('oops', 'oops', [], 'oops', 'url', 'https://')])
273 ));
255 274
256 public function testAssignTagsWithArrayAndExtraSpaces()
257 {
258 $graby = $this->getMockBuilder('Graby\Graby') 275 $graby = $this->getMockBuilder('Graby\Graby')
276 ->setMethods(['fetchContent'])
259 ->disableOriginalConstructor() 277 ->disableOriginalConstructor()
260 ->getMock(); 278 ->getMock();
261 279
262 $tagRepo = $this->getTagRepositoryMock(); 280 $graby->expects($this->any())
263 $proxy = new ContentProxy($graby, $this->getTaggerMock(), $tagRepo, $this->getLogger(), $this->fetchingErrorMessage); 281 ->method('fetchContent')
282 ->willReturn([
283 'html' => str_repeat('this is my content', 325),
284 'title' => 'this is my title',
285 'url' => 'http://1.1.1.1',
286 'content_type' => 'text/html',
287 'language' => 'fr',
288 'status' => '200',
289 'open_graph' => [
290 'og_title' => 'my OG title',
291 'og_description' => 'OG desc',
292 'og_image' => 'https://',
293 ],
294 ]);
264 295
296 $proxy = new ContentProxy($graby, $tagger, $validator, $this->getLogger(), $this->fetchingErrorMessage);
265 $entry = new Entry(new User()); 297 $entry = new Entry(new User());
298 $proxy->updateEntry($entry, 'http://0.0.0.0');
266 299
267 $proxy->assignTagsToEntry($entry, [' tag1', 'tag2 ']); 300 $this->assertEquals('http://1.1.1.1', $entry->getUrl());
268 301 $this->assertEquals('this is my title', $entry->getTitle());
269 $this->assertCount(2, $entry->getTags()); 302 $this->assertContains('this is my content', $entry->getContent());
270 $this->assertEquals('tag1', $entry->getTags()[0]->getLabel()); 303 $this->assertNull($entry->getPreviewPicture());
271 $this->assertEquals('tag2', $entry->getTags()[1]->getLabel()); 304 $this->assertEquals('text/html', $entry->getMimetype());
305 $this->assertEquals('fr', $entry->getLanguage());
306 $this->assertEquals('200', $entry->getHttpStatus());
307 $this->assertEquals(4.0, $entry->getReadingTime());
308 $this->assertEquals('1.1.1.1', $entry->getDomainName());
272 } 309 }
273 310
274 public function testAssignTagsWithString() 311 public function testWithForcedContent()
275 { 312 {
276 $graby = $this->getMockBuilder('Graby\Graby') 313 $tagger = $this->getTaggerMock();
277 ->disableOriginalConstructor() 314 $tagger->expects($this->once())
278 ->getMock(); 315 ->method('tag');
279
280 $tagRepo = $this->getTagRepositoryMock();
281 $proxy = new ContentProxy($graby, $this->getTaggerMock(), $tagRepo, $this->getLogger(), $this->fetchingErrorMessage);
282 316
317 $proxy = new ContentProxy((new Graby()), $tagger, $this->getValidator(), $this->getLogger(), $this->fetchingErrorMessage);
283 $entry = new Entry(new User()); 318 $entry = new Entry(new User());
319 $proxy->updateEntry(
320 $entry,
321 'http://0.0.0.0',
322 [
323 'html' => str_repeat('this is my content', 325),
324 'title' => 'this is my title',
325 'url' => 'http://1.1.1.1',
326 'content_type' => 'text/html',
327 'language' => 'fr',
328 'date' => '1395635872',
329 'authors' => ['Jeremy', 'Nico', 'Thomas'],
330 'all_headers' => [
331 'Cache-Control' => 'no-cache',
332 ],
333 ]
334 );
284 335
285 $proxy->assignTagsToEntry($entry, 'tag1, tag2'); 336 $this->assertEquals('http://1.1.1.1', $entry->getUrl());
286 337 $this->assertEquals('this is my title', $entry->getTitle());
287 $this->assertCount(2, $entry->getTags()); 338 $this->assertContains('this is my content', $entry->getContent());
288 $this->assertEquals('tag1', $entry->getTags()[0]->getLabel()); 339 $this->assertEquals('text/html', $entry->getMimetype());
289 $this->assertEquals('tag2', $entry->getTags()[1]->getLabel()); 340 $this->assertEquals('fr', $entry->getLanguage());
341 $this->assertEquals(4.0, $entry->getReadingTime());
342 $this->assertEquals('1.1.1.1', $entry->getDomainName());
343 $this->assertEquals('24/03/2014', $entry->getPublishedAt()->format('d/m/Y'));
344 $this->assertContains('Jeremy', $entry->getPublishedBy());
345 $this->assertContains('Nico', $entry->getPublishedBy());
346 $this->assertContains('Thomas', $entry->getPublishedBy());
347 $this->assertContains('no-cache', $entry->getHeaders());
290 } 348 }
291 349
292 public function testAssignTagsWithEmptyArray() 350 public function testWithForcedContentAndDatetime()
293 { 351 {
294 $graby = $this->getMockBuilder('Graby\Graby') 352 $tagger = $this->getTaggerMock();
295 ->disableOriginalConstructor() 353 $tagger->expects($this->once())
296 ->getMock(); 354 ->method('tag');
297 355
298 $tagRepo = $this->getTagRepositoryMock(); 356 $logHandler = new TestHandler();
299 $proxy = new ContentProxy($graby, $this->getTaggerMock(), $tagRepo, $this->getLogger(), $this->fetchingErrorMessage); 357 $logger = new Logger('test', [$logHandler]);
300 358
359 $proxy = new ContentProxy((new Graby()), $tagger, $this->getValidator(), $logger, $this->fetchingErrorMessage);
301 $entry = new Entry(new User()); 360 $entry = new Entry(new User());
361 $proxy->updateEntry(
362 $entry,
363 'http://1.1.1.1',
364 [
365 'html' => str_repeat('this is my content', 325),
366 'title' => 'this is my title',
367 'url' => 'http://1.1.1.1',
368 'content_type' => 'text/html',
369 'language' => 'fr',
370 'date' => '2016-09-08T11:55:58+0200',
371 ]
372 );
302 373
303 $proxy->assignTagsToEntry($entry, []); 374 $this->assertEquals('http://1.1.1.1', $entry->getUrl());
304 375 $this->assertEquals('this is my title', $entry->getTitle());
305 $this->assertCount(0, $entry->getTags()); 376 $this->assertContains('this is my content', $entry->getContent());
377 $this->assertEquals('text/html', $entry->getMimetype());
378 $this->assertEquals('fr', $entry->getLanguage());
379 $this->assertEquals(4.0, $entry->getReadingTime());
380 $this->assertEquals('1.1.1.1', $entry->getDomainName());
381 $this->assertEquals('08/09/2016', $entry->getPublishedAt()->format('d/m/Y'));
306 } 382 }
307 383
308 public function testAssignTagsWithEmptyString() 384 public function testWithForcedContentAndBadDate()
309 { 385 {
310 $graby = $this->getMockBuilder('Graby\Graby') 386 $tagger = $this->getTaggerMock();
311 ->disableOriginalConstructor() 387 $tagger->expects($this->once())
312 ->getMock(); 388 ->method('tag');
313 389
314 $tagRepo = $this->getTagRepositoryMock(); 390 $logger = new Logger('foo');
315 $proxy = new ContentProxy($graby, $this->getTaggerMock(), $tagRepo, $this->getLogger(), $this->fetchingErrorMessage); 391 $handler = new TestHandler();
392 $logger->pushHandler($handler);
316 393
394 $proxy = new ContentProxy((new Graby()), $tagger, $this->getValidator(), $logger, $this->fetchingErrorMessage);
317 $entry = new Entry(new User()); 395 $entry = new Entry(new User());
396 $proxy->updateEntry(
397 $entry,
398 'http://1.1.1.1',
399 [
400 'html' => str_repeat('this is my content', 325),
401 'title' => 'this is my title',
402 'url' => 'http://1.1.1.1',
403 'content_type' => 'text/html',
404 'language' => 'fr',
405 'date' => '01 02 2012',
406 ]
407 );
408
409 $this->assertEquals('http://1.1.1.1', $entry->getUrl());
410 $this->assertEquals('this is my title', $entry->getTitle());
411 $this->assertContains('this is my content', $entry->getContent());
412 $this->assertEquals('text/html', $entry->getMimetype());
413 $this->assertEquals('fr', $entry->getLanguage());
414 $this->assertEquals(4.0, $entry->getReadingTime());
415 $this->assertEquals('1.1.1.1', $entry->getDomainName());
416 $this->assertNull($entry->getPublishedAt());
318 417
319 $proxy->assignTagsToEntry($entry, ''); 418 $records = $handler->getRecords();
320 419
321 $this->assertCount(0, $entry->getTags()); 420 $this->assertCount(1, $records);
421 $this->assertContains('Error while defining date', $records[0]['message']);
322 } 422 }
323 423
324 public function testAssignTagsAlreadyAssigned() 424 public function testTaggerThrowException()
325 { 425 {
326 $graby = $this->getMockBuilder('Graby\Graby') 426 $tagger = $this->getTaggerMock();
327 ->disableOriginalConstructor() 427 $tagger->expects($this->once())
328 ->getMock(); 428 ->method('tag')
329 429 ->will($this->throwException(new \Exception()));
330 $tagRepo = $this->getTagRepositoryMock();
331 $proxy = new ContentProxy($graby, $this->getTaggerMock(), $tagRepo, $this->getLogger(), $this->fetchingErrorMessage);
332
333 $tagEntity = new Tag();
334 $tagEntity->setLabel('tag1');
335 430
431 $proxy = new ContentProxy((new Graby()), $tagger, $this->getValidator(), $this->getLogger(), $this->fetchingErrorMessage);
336 $entry = new Entry(new User()); 432 $entry = new Entry(new User());
337 $entry->addTag($tagEntity); 433 $proxy->updateEntry(
338 434 $entry,
339 $proxy->assignTagsToEntry($entry, 'tag1, tag2'); 435 'http://1.1.1.1',
436 [
437 'html' => str_repeat('this is my content', 325),
438 'title' => 'this is my title',
439 'url' => 'http://1.1.1.1',
440 'content_type' => 'text/html',
441 'language' => 'fr',
442 ]
443 );
340 444
341 $this->assertCount(2, $entry->getTags()); 445 $this->assertCount(0, $entry->getTags());
342 $this->assertEquals('tag1', $entry->getTags()[0]->getLabel());
343 $this->assertEquals('tag2', $entry->getTags()[1]->getLabel());
344 } 446 }
345 447
346 public function testAssignTagsNotFlushed() 448 public function dataForCrazyHtml()
347 { 449 {
348 $graby = $this->getMockBuilder('Graby\Graby') 450 return [
349 ->disableOriginalConstructor() 451 'script and comment' => [
350 ->getMock(); 452 '<strong>Script inside:</strong> <!--[if gte IE 4]><script>alert(\'lol\');</script><![endif]--><br />',
351 453 'lol',
352 $tagRepo = $this->getTagRepositoryMock(); 454 ],
353 $tagRepo->expects($this->never()) 455 'script' => [
354 ->method('__call'); 456 '<strong>Script inside:</strong><script>alert(\'lol\');</script>',
355 457 'script',
356 $proxy = new ContentProxy($graby, $this->getTaggerMock(), $tagRepo, $this->getLogger(), $this->fetchingErrorMessage); 458 ],
459 ];
460 }
357 461
358 $tagEntity = new Tag(); 462 /**
359 $tagEntity->setLabel('tag1'); 463 * @dataProvider dataForCrazyHtml
464 */
465 public function testWithCrazyHtmlContent($html, $escapedString)
466 {
467 $tagger = $this->getTaggerMock();
468 $tagger->expects($this->once())
469 ->method('tag');
360 470
471 $proxy = new ContentProxy((new Graby()), $tagger, $this->getValidator(), $this->getLogger(), $this->fetchingErrorMessage);
361 $entry = new Entry(new User()); 472 $entry = new Entry(new User());
473 $proxy->updateEntry(
474 $entry,
475 'http://1.1.1.1',
476 [
477 'html' => $html,
478 'title' => 'this is my title',
479 'url' => 'http://1.1.1.1',
480 'content_type' => 'text/html',
481 'language' => 'fr',
482 'status' => '200',
483 'open_graph' => [
484 'og_title' => 'my OG title',
485 'og_description' => 'OG desc',
486 'og_image' => 'http://3.3.3.3/cover.jpg',
487 ],
488 ]
489 );
362 490
363 $proxy->assignTagsToEntry($entry, 'tag1', [$tagEntity]); 491 $this->assertEquals('http://1.1.1.1', $entry->getUrl());
364 492 $this->assertEquals('this is my title', $entry->getTitle());
365 $this->assertCount(1, $entry->getTags()); 493 $this->assertNotContains($escapedString, $entry->getContent());
366 $this->assertEquals('tag1', $entry->getTags()[0]->getLabel()); 494 $this->assertEquals('http://3.3.3.3/cover.jpg', $entry->getPreviewPicture());
495 $this->assertEquals('text/html', $entry->getMimetype());
496 $this->assertEquals('fr', $entry->getLanguage());
497 $this->assertEquals('200', $entry->getHttpStatus());
498 $this->assertEquals('1.1.1.1', $entry->getDomainName());
367 } 499 }
368 500
369 private function getTaggerMock() 501 private function getTaggerMock()
370 { 502 {
371 return $this->getMockBuilder('Wallabag\CoreBundle\Helper\RuleBasedTagger') 503 return $this->getMockBuilder(RuleBasedTagger::class)
372 ->setMethods(['tag']) 504 ->setMethods(['tag'])
373 ->disableOriginalConstructor() 505 ->disableOriginalConstructor()
374 ->getMock(); 506 ->getMock();
375 } 507 }
376 508
377 private function getTagRepositoryMock() 509 private function getLogger()
378 { 510 {
379 return $this->getMockBuilder('Wallabag\CoreBundle\Repository\TagRepository') 511 return new NullLogger();
380 ->disableOriginalConstructor()
381 ->getMock();
382 } 512 }
383 513
384 private function getLogger() 514 private function getValidator()
385 { 515 {
386 return new NullLogger(); 516 return $this->getMockBuilder(RecursiveValidator::class)
517 ->setMethods(['validate'])
518 ->disableOriginalConstructor()
519 ->getMock();
387 } 520 }
388} 521}
diff --git a/tests/Wallabag/CoreBundle/Helper/CryptoProxyTest.php b/tests/Wallabag/CoreBundle/Helper/CryptoProxyTest.php
new file mode 100644
index 00000000..cede8696
--- /dev/null
+++ b/tests/Wallabag/CoreBundle/Helper/CryptoProxyTest.php
@@ -0,0 +1,40 @@
1<?php
2
3namespace Tests\Wallabag\CoreBundle\Helper;
4
5use Psr\Log\NullLogger;
6use Monolog\Logger;
7use Monolog\Handler\TestHandler;
8use Wallabag\CoreBundle\Helper\CryptoProxy;
9
10class CryptoProxyTest extends \PHPUnit_Framework_TestCase
11{
12 public function testCrypto()
13 {
14 $logHandler = new TestHandler();
15 $logger = new Logger('test', [$logHandler]);
16
17 $crypto = new CryptoProxy(sys_get_temp_dir().'/'.uniqid('', true).'.txt', $logger);
18 $crypted = $crypto->crypt('test');
19 $decrypted = $crypto->decrypt($crypted);
20
21 $this->assertSame('test', $decrypted);
22
23 $records = $logHandler->getRecords();
24 $this->assertCount(2, $records);
25 $this->assertContains('Crypto: crypting value', $records[0]['message']);
26 $this->assertContains('Crypto: decrypting value', $records[1]['message']);
27 }
28
29 /**
30 * @expectedException RuntimeException
31 * @expectedExceptionMessage Decrypt fail
32 *
33 * @return [type] [description]
34 */
35 public function testDecryptBadValue()
36 {
37 $crypto = new CryptoProxy(sys_get_temp_dir().'/'.uniqid('', true).'.txt', new NullLogger());
38 $crypto->decrypt('badvalue');
39 }
40}
diff --git a/tests/Wallabag/CoreBundle/Helper/DownloadImagesTest.php b/tests/Wallabag/CoreBundle/Helper/DownloadImagesTest.php
index 85f12d87..c02f9658 100644
--- a/tests/Wallabag/CoreBundle/Helper/DownloadImagesTest.php
+++ b/tests/Wallabag/CoreBundle/Helper/DownloadImagesTest.php
@@ -12,7 +12,24 @@ use GuzzleHttp\Stream\Stream;
12 12
13class DownloadImagesTest extends \PHPUnit_Framework_TestCase 13class DownloadImagesTest extends \PHPUnit_Framework_TestCase
14{ 14{
15 public function testProcessHtml() 15 public function dataForSuccessImage()
16 {
17 return [
18 'imgur' => [
19 '<div><img src="http://i.imgur.com/T9qgcHc.jpg" /></div>',
20 'http://imgur.com/gallery/WxtWY',
21 ],
22 'image with &' => [
23 '<div><img src="https://i2.wp.com/www.tvaddons.ag/wp-content/uploads/2017/01/Screen-Shot-2017-01-07-at-10.17.40-PM.jpg?w=640&amp;ssl=1" /></div>',
24 'https://www.tvaddons.ag/realdebrid-kodi-jarvis/',
25 ],
26 ];
27 }
28
29 /**
30 * @dataProvider dataForSuccessImage
31 */
32 public function testProcessHtml($html, $url)
16 { 33 {
17 $client = new Client(); 34 $client = new Client();
18 35
@@ -27,9 +44,10 @@ class DownloadImagesTest extends \PHPUnit_Framework_TestCase
27 44
28 $download = new DownloadImages($client, sys_get_temp_dir().'/wallabag_test', 'http://wallabag.io/', $logger); 45 $download = new DownloadImages($client, sys_get_temp_dir().'/wallabag_test', 'http://wallabag.io/', $logger);
29 46
30 $res = $download->processHtml(123, '<div><img src="http://i.imgur.com/T9qgcHc.jpg" /></div>', 'http://imgur.com/gallery/WxtWY'); 47 $res = $download->processHtml(123, $html, $url);
31 48
32 $this->assertContains('http://wallabag.io/assets/images/9/b/9b0ead26/c638b4c2.png', $res); 49 // this the base path of all image (since it's calculated using the entry id: 123)
50 $this->assertContains('http://wallabag.io/assets/images/9/b/9b0ead26/', $res);
33 } 51 }
34 52
35 public function testProcessHtmlWithBadImage() 53 public function testProcessHtmlWithBadImage()
@@ -139,4 +157,29 @@ class DownloadImagesTest extends \PHPUnit_Framework_TestCase
139 157
140 $this->assertFalse($res, 'Absolute image can not be determined, so it will not be replaced'); 158 $this->assertFalse($res, 'Absolute image can not be determined, so it will not be replaced');
141 } 159 }
160
161 public function testProcessRealImage()
162 {
163 $client = new Client();
164
165 $mock = new Mock([
166 new Response(200, ['content-type' => null], Stream::factory(file_get_contents(__DIR__.'/../fixtures/image-no-content-type.jpg'))),
167 ]);
168
169 $client->getEmitter()->attach($mock);
170
171 $logHandler = new TestHandler();
172 $logger = new Logger('test', array($logHandler));
173
174 $download = new DownloadImages($client, sys_get_temp_dir().'/wallabag_test', 'http://wallabag.io/', $logger);
175
176 $res = $download->processSingleImage(
177 123,
178 'https://cdn.theconversation.com/files/157200/article/width926/gsj2rjp2-1487348607.jpg',
179 'https://theconversation.com/conversation-avec-gerald-bronner-ce-nest-pas-la-post-verite-qui-nous-menace-mais-lextension-de-notre-credulite-73089'
180 );
181
182 $this->assertContains('http://wallabag.io/assets/images/9/b/9b0ead26/', $res, 'Content-Type was empty but data is ok for an image');
183 $this->assertContains('DownloadImages: Checking extension (alternative)', $logHandler->getRecords()[3]['message']);
184 }
142} 185}
diff --git a/tests/Wallabag/CoreBundle/Helper/RedirectTest.php b/tests/Wallabag/CoreBundle/Helper/RedirectTest.php
index 0539f20a..f420d06a 100644
--- a/tests/Wallabag/CoreBundle/Helper/RedirectTest.php
+++ b/tests/Wallabag/CoreBundle/Helper/RedirectTest.php
@@ -89,4 +89,22 @@ class RedirectTest extends \PHPUnit_Framework_TestCase
89 89
90 $this->assertEquals($this->routerMock->generate('homepage'), $redirectUrl); 90 $this->assertEquals($this->routerMock->generate('homepage'), $redirectUrl);
91 } 91 }
92
93 public function testUserForRedirectWithIgnoreActionMarkAsRead()
94 {
95 $this->token->getUser()->getConfig()->setActionMarkAsRead(Config::REDIRECT_TO_HOMEPAGE);
96
97 $redirectUrl = $this->redirect->to('/unread/list', '', true);
98
99 $this->assertEquals('/unread/list', $redirectUrl);
100 }
101
102 public function testUserForRedirectNullWithFallbackWithIgnoreActionMarkAsRead()
103 {
104 $this->token->getUser()->getConfig()->setActionMarkAsRead(Config::REDIRECT_TO_HOMEPAGE);
105
106 $redirectUrl = $this->redirect->to(null, 'fallback', true);
107
108 $this->assertEquals('fallback', $redirectUrl);
109 }
92} 110}
diff --git a/tests/Wallabag/CoreBundle/Helper/RuleBasedTaggerTest.php b/tests/Wallabag/CoreBundle/Helper/RuleBasedTaggerTest.php
index 17b08c2a..1e21f400 100644
--- a/tests/Wallabag/CoreBundle/Helper/RuleBasedTaggerTest.php
+++ b/tests/Wallabag/CoreBundle/Helper/RuleBasedTaggerTest.php
@@ -2,6 +2,8 @@
2 2
3namespace Tests\Wallabag\CoreBundle\Helper; 3namespace Tests\Wallabag\CoreBundle\Helper;
4 4
5use Monolog\Handler\TestHandler;
6use Monolog\Logger;
5use Wallabag\CoreBundle\Entity\Config; 7use Wallabag\CoreBundle\Entity\Config;
6use Wallabag\CoreBundle\Entity\Entry; 8use Wallabag\CoreBundle\Entity\Entry;
7use Wallabag\CoreBundle\Entity\Tag; 9use Wallabag\CoreBundle\Entity\Tag;
@@ -15,14 +17,19 @@ class RuleBasedTaggerTest extends \PHPUnit_Framework_TestCase
15 private $tagRepository; 17 private $tagRepository;
16 private $entryRepository; 18 private $entryRepository;
17 private $tagger; 19 private $tagger;
20 private $logger;
21 private $handler;
18 22
19 public function setUp() 23 public function setUp()
20 { 24 {
21 $this->rulerz = $this->getRulerZMock(); 25 $this->rulerz = $this->getRulerZMock();
22 $this->tagRepository = $this->getTagRepositoryMock(); 26 $this->tagRepository = $this->getTagRepositoryMock();
23 $this->entryRepository = $this->getEntryRepositoryMock(); 27 $this->entryRepository = $this->getEntryRepositoryMock();
28 $this->logger = $this->getLogger();
29 $this->handler = new TestHandler();
30 $this->logger->pushHandler($this->handler);
24 31
25 $this->tagger = new RuleBasedTagger($this->rulerz, $this->tagRepository, $this->entryRepository); 32 $this->tagger = new RuleBasedTagger($this->rulerz, $this->tagRepository, $this->entryRepository, $this->logger);
26 } 33 }
27 34
28 public function testTagWithNoRule() 35 public function testTagWithNoRule()
@@ -32,6 +39,8 @@ class RuleBasedTaggerTest extends \PHPUnit_Framework_TestCase
32 $this->tagger->tag($entry); 39 $this->tagger->tag($entry);
33 40
34 $this->assertTrue($entry->getTags()->isEmpty()); 41 $this->assertTrue($entry->getTags()->isEmpty());
42 $records = $this->handler->getRecords();
43 $this->assertCount(0, $records);
35 } 44 }
36 45
37 public function testTagWithNoMatchingRule() 46 public function testTagWithNoMatchingRule()
@@ -49,6 +58,8 @@ class RuleBasedTaggerTest extends \PHPUnit_Framework_TestCase
49 $this->tagger->tag($entry); 58 $this->tagger->tag($entry);
50 59
51 $this->assertTrue($entry->getTags()->isEmpty()); 60 $this->assertTrue($entry->getTags()->isEmpty());
61 $records = $this->handler->getRecords();
62 $this->assertCount(0, $records);
52 } 63 }
53 64
54 public function testTagWithAMatchingRule() 65 public function testTagWithAMatchingRule()
@@ -70,6 +81,9 @@ class RuleBasedTaggerTest extends \PHPUnit_Framework_TestCase
70 $tags = $entry->getTags(); 81 $tags = $entry->getTags();
71 $this->assertSame('foo', $tags[0]->getLabel()); 82 $this->assertSame('foo', $tags[0]->getLabel());
72 $this->assertSame('bar', $tags[1]->getLabel()); 83 $this->assertSame('bar', $tags[1]->getLabel());
84
85 $records = $this->handler->getRecords();
86 $this->assertCount(1, $records);
73 } 87 }
74 88
75 public function testTagWithAMixOfMatchingRules() 89 public function testTagWithAMixOfMatchingRules()
@@ -90,6 +104,8 @@ class RuleBasedTaggerTest extends \PHPUnit_Framework_TestCase
90 104
91 $tags = $entry->getTags(); 105 $tags = $entry->getTags();
92 $this->assertSame('foo', $tags[0]->getLabel()); 106 $this->assertSame('foo', $tags[0]->getLabel());
107 $records = $this->handler->getRecords();
108 $this->assertCount(1, $records);
93 } 109 }
94 110
95 public function testWhenTheTagExists() 111 public function testWhenTheTagExists()
@@ -118,6 +134,8 @@ class RuleBasedTaggerTest extends \PHPUnit_Framework_TestCase
118 134
119 $tags = $entry->getTags(); 135 $tags = $entry->getTags();
120 $this->assertSame($tag, $tags[0]); 136 $this->assertSame($tag, $tags[0]);
137 $records = $this->handler->getRecords();
138 $this->assertCount(1, $records);
121 } 139 }
122 140
123 public function testSameTagWithDifferentfMatchingRules() 141 public function testSameTagWithDifferentfMatchingRules()
@@ -138,6 +156,8 @@ class RuleBasedTaggerTest extends \PHPUnit_Framework_TestCase
138 156
139 $tags = $entry->getTags(); 157 $tags = $entry->getTags();
140 $this->assertCount(1, $tags); 158 $this->assertCount(1, $tags);
159 $records = $this->handler->getRecords();
160 $this->assertCount(2, $records);
141 } 161 }
142 162
143 public function testTagAllEntriesForAUser() 163 public function testTagAllEntriesForAUser()
@@ -209,4 +229,9 @@ class RuleBasedTaggerTest extends \PHPUnit_Framework_TestCase
209 ->disableOriginalConstructor() 229 ->disableOriginalConstructor()
210 ->getMock(); 230 ->getMock();
211 } 231 }
232
233 private function getLogger()
234 {
235 return new Logger('foo');
236 }
212} 237}
diff --git a/tests/Wallabag/CoreBundle/Helper/TagsAssignerTest.php b/tests/Wallabag/CoreBundle/Helper/TagsAssignerTest.php
new file mode 100644
index 00000000..6d6d6484
--- /dev/null
+++ b/tests/Wallabag/CoreBundle/Helper/TagsAssignerTest.php
@@ -0,0 +1,108 @@
1<?php
2
3namespace Tests\Wallabag\CoreBundle\Helper;
4
5use Wallabag\CoreBundle\Entity\Entry;
6use Wallabag\CoreBundle\Entity\Tag;
7use Wallabag\CoreBundle\Helper\TagsAssigner;
8use Wallabag\UserBundle\Entity\User;
9use Wallabag\CoreBundle\Repository\TagRepository;
10
11class TagsAssignerTest extends \PHPUnit_Framework_TestCase
12{
13 public function testAssignTagsWithArrayAndExtraSpaces()
14 {
15 $tagRepo = $this->getTagRepositoryMock();
16 $tagsAssigner = new TagsAssigner($tagRepo);
17
18 $entry = new Entry(new User());
19
20 $tagsAssigner->assignTagsToEntry($entry, [' tag1', 'tag2 ']);
21
22 $this->assertCount(2, $entry->getTags());
23 $this->assertEquals('tag1', $entry->getTags()[0]->getLabel());
24 $this->assertEquals('tag2', $entry->getTags()[1]->getLabel());
25 }
26
27 public function testAssignTagsWithString()
28 {
29 $tagRepo = $this->getTagRepositoryMock();
30 $tagsAssigner = new TagsAssigner($tagRepo);
31
32 $entry = new Entry(new User());
33
34 $tagsAssigner->assignTagsToEntry($entry, 'tag1, tag2');
35
36 $this->assertCount(2, $entry->getTags());
37 $this->assertEquals('tag1', $entry->getTags()[0]->getLabel());
38 $this->assertEquals('tag2', $entry->getTags()[1]->getLabel());
39 }
40
41 public function testAssignTagsWithEmptyArray()
42 {
43 $tagRepo = $this->getTagRepositoryMock();
44 $tagsAssigner = new TagsAssigner($tagRepo);
45
46 $entry = new Entry(new User());
47
48 $tagsAssigner->assignTagsToEntry($entry, []);
49
50 $this->assertCount(0, $entry->getTags());
51 }
52
53 public function testAssignTagsWithEmptyString()
54 {
55 $tagRepo = $this->getTagRepositoryMock();
56 $tagsAssigner = new TagsAssigner($tagRepo);
57
58 $entry = new Entry(new User());
59
60 $tagsAssigner->assignTagsToEntry($entry, '');
61
62 $this->assertCount(0, $entry->getTags());
63 }
64
65 public function testAssignTagsAlreadyAssigned()
66 {
67 $tagRepo = $this->getTagRepositoryMock();
68 $tagsAssigner = new TagsAssigner($tagRepo);
69
70 $tagEntity = new Tag();
71 $tagEntity->setLabel('tag1');
72
73 $entry = new Entry(new User());
74 $entry->addTag($tagEntity);
75
76 $tagsAssigner->assignTagsToEntry($entry, 'tag1, tag2');
77
78 $this->assertCount(2, $entry->getTags());
79 $this->assertEquals('tag1', $entry->getTags()[0]->getLabel());
80 $this->assertEquals('tag2', $entry->getTags()[1]->getLabel());
81 }
82
83 public function testAssignTagsNotFlushed()
84 {
85 $tagRepo = $this->getTagRepositoryMock();
86 $tagRepo->expects($this->never())
87 ->method('__call');
88
89 $tagsAssigner = new TagsAssigner($tagRepo);
90
91 $tagEntity = new Tag();
92 $tagEntity->setLabel('tag1');
93
94 $entry = new Entry(new User());
95
96 $tagsAssigner->assignTagsToEntry($entry, 'tag1', [$tagEntity]);
97
98 $this->assertCount(1, $entry->getTags());
99 $this->assertEquals('tag1', $entry->getTags()[0]->getLabel());
100 }
101
102 private function getTagRepositoryMock()
103 {
104 return $this->getMockBuilder(TagRepository::class)
105 ->disableOriginalConstructor()
106 ->getMock();
107 }
108}
diff --git a/tests/Wallabag/CoreBundle/ParamConverter/UsernameRssTokenConverterTest.php b/tests/Wallabag/CoreBundle/ParamConverter/UsernameRssTokenConverterTest.php
index 2e6fccfb..ca8e0d50 100644
--- a/tests/Wallabag/CoreBundle/ParamConverter/UsernameRssTokenConverterTest.php
+++ b/tests/Wallabag/CoreBundle/ParamConverter/UsernameRssTokenConverterTest.php
@@ -136,7 +136,7 @@ class UsernameRssTokenConverterTest extends \PHPUnit_Framework_TestCase
136 } 136 }
137 137
138 /** 138 /**
139 * @expectedException Symfony\Component\HttpKernel\Exception\NotFoundHttpException 139 * @expectedException \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
140 * @expectedExceptionMessage User not found 140 * @expectedExceptionMessage User not found
141 */ 141 */
142 public function testApplyUserNotFound() 142 public function testApplyUserNotFound()
diff --git a/tests/Wallabag/CoreBundle/WallabagCoreTestCase.php b/tests/Wallabag/CoreBundle/WallabagCoreTestCase.php
index 7bf4b43c..eec6939d 100644
--- a/tests/Wallabag/CoreBundle/WallabagCoreTestCase.php
+++ b/tests/Wallabag/CoreBundle/WallabagCoreTestCase.php
@@ -2,11 +2,20 @@
2 2
3namespace Tests\Wallabag\CoreBundle; 3namespace Tests\Wallabag\CoreBundle;
4 4
5use Symfony\Bundle\FrameworkBundle\Client;
6use Symfony\Bundle\FrameworkBundle\Console\Application;
5use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; 7use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
6use Symfony\Component\BrowserKit\Cookie; 8use Symfony\Component\BrowserKit\Cookie;
9use Symfony\Component\Console\Input\ArrayInput;
10use Symfony\Component\Console\Output\NullOutput;
11use Wallabag\CoreBundle\Entity\Config;
12use Wallabag\UserBundle\Entity\User;
7 13
8abstract class WallabagCoreTestCase extends WebTestCase 14abstract class WallabagCoreTestCase extends WebTestCase
9{ 15{
16 /**
17 * @var Client|null
18 */
10 private $client = null; 19 private $client = null;
11 20
12 public function getClient() 21 public function getClient()
@@ -21,6 +30,44 @@ abstract class WallabagCoreTestCase extends WebTestCase
21 $this->client = static::createClient(); 30 $this->client = static::createClient();
22 } 31 }
23 32
33 public function resetDatabase(Client $client)
34 {
35 $application = new Application($client->getKernel());
36 $application->setAutoExit(false);
37
38 $application->run(new ArrayInput([
39 'command' => 'doctrine:schema:drop',
40 '--no-interaction' => true,
41 '--force' => true,
42 '--env' => 'test',
43 ]), new NullOutput());
44
45 $application->run(new ArrayInput([
46 'command' => 'doctrine:schema:create',
47 '--no-interaction' => true,
48 '--env' => 'test',
49 ]), new NullOutput());
50
51 $application->run(new ArrayInput([
52 'command' => 'doctrine:fixtures:load',
53 '--no-interaction' => true,
54 '--env' => 'test',
55 ]), new NullOutput());
56
57 /*
58 * Recreate client to avoid error:
59 *
60 * [Doctrine\DBAL\ConnectionException]
61 * Transaction commit failed because the transaction has been marked for rollback only.
62 */
63 $this->client = static::createClient();
64 }
65
66 public function getEntityManager()
67 {
68 return $this->client->getContainer()->get('doctrine.orm.entity_manager');
69 }
70
24 /** 71 /**
25 * Login a user without making a HTTP request. 72 * Login a user without making a HTTP request.
26 * If we make a HTTP request we lose ability to mock service in the container. 73 * If we make a HTTP request we lose ability to mock service in the container.
@@ -37,7 +84,7 @@ abstract class WallabagCoreTestCase extends WebTestCase
37 $firewallName = $container->getParameter('fos_user.firewall_name'); 84 $firewallName = $container->getParameter('fos_user.firewall_name');
38 85
39 $user = $userManager->findUserBy(array('username' => $username)); 86 $user = $userManager->findUserBy(array('username' => $username));
40 $loginManager->loginUser($firewallName, $user); 87 $loginManager->logInUser($firewallName, $user);
41 88
42 $session->set('_security_'.$firewallName, serialize($container->get('security.token_storage')->getToken())); 89 $session->set('_security_'.$firewallName, serialize($container->get('security.token_storage')->getToken()));
43 $session->save(); 90 $session->save();
@@ -65,23 +112,42 @@ abstract class WallabagCoreTestCase extends WebTestCase
65 } 112 }
66 113
67 /** 114 /**
68 * Return the user id of the logged in user. 115 * Return the user of the logged in user.
69 * You should be sure that you called `logInAs` before. 116 * You should be sure that you called `logInAs` before.
70 * 117 *
71 * @return int 118 * @return User
72 */ 119 */
73 public function getLoggedInUserId() 120 public function getLoggedInUser()
74 { 121 {
75 $token = static::$kernel->getContainer()->get('security.token_storage')->getToken(); 122 $token = static::$kernel->getContainer()->get('security.token_storage')->getToken();
76 123
77 if (null !== $token) { 124 if (null !== $token) {
78 return $token->getUser()->getId(); 125 return $token->getUser();
79 } 126 }
80 127
81 throw new \RuntimeException('No logged in User.'); 128 throw new \RuntimeException('No logged in User.');
82 } 129 }
83 130
84 /** 131 /**
132 * Return the user id of the logged in user.
133 * You should be sure that you called `logInAs` before.
134 *
135 * @return int
136 */
137 public function getLoggedInUserId()
138 {
139 return $this->getLoggedInUser()->getId();
140 }
141
142 public function useTheme($theme)
143 {
144 $config = $this->getEntityManager()->getRepository(Config::class)->findOneByUser($this->getLoggedInUser());
145 $config->setTheme($theme);
146 $this->getEntityManager()->persist($config);
147 $this->getEntityManager()->flush();
148 }
149
150 /**
85 * Check if Redis is installed. 151 * Check if Redis is installed.
86 * If not, mark test as skip. 152 * If not, mark test as skip.
87 */ 153 */
diff --git a/tests/Wallabag/CoreBundle/fixtures/image-no-content-type.jpg b/tests/Wallabag/CoreBundle/fixtures/image-no-content-type.jpg
new file mode 100644
index 00000000..0c60e952
--- /dev/null
+++ b/tests/Wallabag/CoreBundle/fixtures/image-no-content-type.jpg
Binary files differ