]>
Commit | Line | Data |
---|---|---|
bfe02a0b TC |
1 | <?php |
2 | ||
3 | namespace Tests\Wallabag\CoreBundle\Command; | |
4 | ||
5 | use Symfony\Bundle\FrameworkBundle\Console\Application; | |
6 | use Symfony\Component\Console\Tester\CommandTester; | |
7 | use Tests\Wallabag\CoreBundle\WallabagCoreTestCase; | |
8 | use Wallabag\CoreBundle\Command\GenerateUrlHashesCommand; | |
9 | use Wallabag\CoreBundle\Entity\Entry; | |
10 | ||
11 | class GenerateUrlHashesCommandTest extends WallabagCoreTestCase | |
12 | { | |
13 | public function testRunGenerateUrlHashesCommand() | |
14 | { | |
15 | $application = new Application($this->getClient()->getKernel()); | |
16 | $application->add(new GenerateUrlHashesCommand()); | |
17 | ||
18 | $command = $application->find('wallabag:generate-hashed-urls'); | |
19 | ||
20 | $tester = new CommandTester($command); | |
21 | $tester->execute([ | |
22 | 'command' => $command->getName(), | |
23 | ]); | |
24 | ||
9c2b2aae | 25 | $this->assertContains('Generating hashed urls for "3" users', $tester->getDisplay()); |
bfe02a0b TC |
26 | $this->assertContains('Finished generated hashed urls', $tester->getDisplay()); |
27 | } | |
28 | ||
29 | public function testRunGenerateUrlHashesCommandWithBadUsername() | |
30 | { | |
31 | $application = new Application($this->getClient()->getKernel()); | |
32 | $application->add(new GenerateUrlHashesCommand()); | |
33 | ||
34 | $command = $application->find('wallabag:generate-hashed-urls'); | |
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 testRunGenerateUrlHashesCommandForUser() | |
46 | { | |
47 | $application = new Application($this->getClient()->getKernel()); | |
48 | $application->add(new GenerateUrlHashesCommand()); | |
49 | ||
50 | $command = $application->find('wallabag:generate-hashed-urls'); | |
51 | ||
52 | $tester = new CommandTester($command); | |
53 | $tester->execute([ | |
54 | 'command' => $command->getName(), | |
55 | 'username' => 'admin', | |
56 | ]); | |
57 | ||
9c2b2aae | 58 | $this->assertContains('Generated hashed urls for user: admin', $tester->getDisplay()); |
bfe02a0b TC |
59 | } |
60 | ||
61 | public function testGenerateUrls() | |
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 | $user = $em->getRepository('WallabagUserBundle:User')->findOneById($this->getLoggedInUserId()); | |
70 | ||
71 | $entry1 = new Entry($user); | |
72 | $entry1->setUrl($url); | |
73 | ||
74 | $em->persist($entry1); | |
bfe02a0b TC |
75 | $em->flush(); |
76 | ||
bfe02a0b TC |
77 | $application = new Application($this->getClient()->getKernel()); |
78 | $application->add(new GenerateUrlHashesCommand()); | |
79 | ||
80 | $command = $application->find('wallabag:generate-hashed-urls'); | |
81 | ||
82 | $tester = new CommandTester($command); | |
83 | $tester->execute([ | |
84 | 'command' => $command->getName(), | |
85 | 'username' => 'admin', | |
86 | ]); | |
87 | ||
9c2b2aae | 88 | $this->assertContains('Generated hashed urls for user: admin', $tester->getDisplay()); |
bfe02a0b TC |
89 | |
90 | $entry = $em->getRepository('WallabagCoreBundle:Entry')->findOneByUrl($url); | |
91 | ||
8a645662 | 92 | $this->assertSame($entry->getHashedUrl(), hash('sha1', $url)); |
bfe02a0b TC |
93 | |
94 | $query = $em->createQuery('DELETE FROM Wallabag\CoreBundle\Entity\Entry e WHERE e.url = :url'); | |
95 | $query->setParameter('url', $url); | |
96 | $query->execute(); | |
97 | } | |
98 | } |