diff options
-rw-r--r-- | src/Wallabag/CoreBundle/Command/ShowUserCommand.php | 77 | ||||
-rw-r--r-- | tests/Wallabag/CoreBundle/Command/ShowUserCommandTest.php | 95 |
2 files changed, 172 insertions, 0 deletions
diff --git a/src/Wallabag/CoreBundle/Command/ShowUserCommand.php b/src/Wallabag/CoreBundle/Command/ShowUserCommand.php new file mode 100644 index 00000000..10428c4b --- /dev/null +++ b/src/Wallabag/CoreBundle/Command/ShowUserCommand.php | |||
@@ -0,0 +1,77 @@ | |||
1 | <?php | ||
2 | |||
3 | namespace Wallabag\CoreBundle\Command; | ||
4 | |||
5 | use Doctrine\ORM\NoResultException; | ||
6 | use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; | ||
7 | use Symfony\Component\Console\Input\InputArgument; | ||
8 | use Symfony\Component\Console\Input\InputInterface; | ||
9 | use Symfony\Component\Console\Output\OutputInterface; | ||
10 | use Wallabag\UserBundle\Entity\User; | ||
11 | |||
12 | class ShowUserCommand extends ContainerAwareCommand | ||
13 | { | ||
14 | /** @var OutputInterface */ | ||
15 | protected $output; | ||
16 | |||
17 | protected function configure() | ||
18 | { | ||
19 | $this | ||
20 | ->setName('wallabag:user:show') | ||
21 | ->setDescription('Show user details') | ||
22 | ->setHelp('This command shows the details for an user') | ||
23 | ->addArgument( | ||
24 | 'username', | ||
25 | InputArgument::REQUIRED, | ||
26 | 'User to show details for' | ||
27 | ); | ||
28 | } | ||
29 | |||
30 | protected function execute(InputInterface $input, OutputInterface $output) | ||
31 | { | ||
32 | $this->output = $output; | ||
33 | |||
34 | $username = $input->getArgument('username'); | ||
35 | |||
36 | try { | ||
37 | $user = $this->getUser($username); | ||
38 | $this->showUser($user); | ||
39 | } catch (NoResultException $e) { | ||
40 | $output->writeln(sprintf('<error>User "%s" not found.</error>', $username)); | ||
41 | |||
42 | return 1; | ||
43 | } | ||
44 | |||
45 | return 0; | ||
46 | } | ||
47 | |||
48 | /** | ||
49 | * @param User $user | ||
50 | */ | ||
51 | private function showUser(User $user) | ||
52 | { | ||
53 | $this->output->writeln(sprintf('Username : %s', $user->getUsername())); | ||
54 | $this->output->writeln(sprintf('Email : %s', $user->getEmail())); | ||
55 | $this->output->writeln(sprintf('Display name : %s', $user->getName())); | ||
56 | $this->output->writeln(sprintf('Creation date : %s', $user->getCreatedAt() !== null ? $user->getCreatedAt()->format('Y-m-d H:i:s') : 'false')); | ||
57 | $this->output->writeln(sprintf('Last login : %s', $user->getLastLogin() !== null ? $user->getLastLogin()->format('Y-m-d H:i:s') : 'false')); | ||
58 | $this->output->writeln(sprintf('2FA activated: %s', $user->isTwoFactorAuthentication() ? 'true' : 'false')); | ||
59 | } | ||
60 | |||
61 | /** | ||
62 | * Fetches a user from its username. | ||
63 | * | ||
64 | * @param string $username | ||
65 | * | ||
66 | * @return \Wallabag\UserBundle\Entity\User | ||
67 | */ | ||
68 | private function getUser($username) | ||
69 | { | ||
70 | return $this->getDoctrine()->getRepository('WallabagUserBundle:User')->findOneByUserName($username); | ||
71 | } | ||
72 | |||
73 | private function getDoctrine() | ||
74 | { | ||
75 | return $this->getContainer()->get('doctrine'); | ||
76 | } | ||
77 | } | ||
diff --git a/tests/Wallabag/CoreBundle/Command/ShowUserCommandTest.php b/tests/Wallabag/CoreBundle/Command/ShowUserCommandTest.php new file mode 100644 index 00000000..642327c3 --- /dev/null +++ b/tests/Wallabag/CoreBundle/Command/ShowUserCommandTest.php | |||
@@ -0,0 +1,95 @@ | |||
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 Wallabag\CoreBundle\Command\CleanDuplicatesCommand; | ||
8 | use Tests\Wallabag\CoreBundle\WallabagCoreTestCase; | ||
9 | use Wallabag\CoreBundle\Command\ShowUserCommand; | ||
10 | use Wallabag\CoreBundle\Entity\Entry; | ||
11 | use Wallabag\UserBundle\Entity\User; | ||
12 | |||
13 | class ShowUserCommandTest extends WallabagCoreTestCase | ||
14 | { | ||
15 | /** | ||
16 | * @expectedException \Symfony\Component\Console\Exception\RuntimeException | ||
17 | * @expectedExceptionMessage Not enough arguments | ||
18 | */ | ||
19 | public function testRunShowUserCommandWithoutUsername() | ||
20 | { | ||
21 | $application = new Application($this->getClient()->getKernel()); | ||
22 | $application->add(new ShowUserCommand()); | ||
23 | |||
24 | $command = $application->find('wallabag:user:show'); | ||
25 | |||
26 | $tester = new CommandTester($command); | ||
27 | $tester->execute([ | ||
28 | 'command' => $command->getName(), | ||
29 | ]); | ||
30 | } | ||
31 | |||
32 | public function testRunShowUserCommandWithBadUsername() | ||
33 | { | ||
34 | $application = new Application($this->getClient()->getKernel()); | ||
35 | $application->add(new ShowUserCommand()); | ||
36 | |||
37 | $command = $application->find('wallabag:user:show'); | ||
38 | |||
39 | $tester = new CommandTester($command); | ||
40 | $tester->execute([ | ||
41 | 'command' => $command->getName(), | ||
42 | 'username' => 'unknown', | ||
43 | ]); | ||
44 | |||
45 | $this->assertContains('User "unknown" not found', $tester->getDisplay()); | ||
46 | } | ||
47 | |||
48 | public function testRunShowUserCommandForUser() | ||
49 | { | ||
50 | $application = new Application($this->getClient()->getKernel()); | ||
51 | $application->add(new ShowUserCommand()); | ||
52 | |||
53 | $command = $application->find('wallabag:user:show'); | ||
54 | |||
55 | $tester = new CommandTester($command); | ||
56 | $tester->execute([ | ||
57 | 'command' => $command->getName(), | ||
58 | 'username' => 'admin', | ||
59 | ]); | ||
60 | |||
61 | $this->assertContains('Username : admin', $tester->getDisplay()); | ||
62 | $this->assertContains('Email : bigboss@wallabag.org', $tester->getDisplay()); | ||
63 | $this->assertContains('Display name : Big boss', $tester->getDisplay()); | ||
64 | $this->assertContains('2FA activated: false', $tester->getDisplay()); | ||
65 | } | ||
66 | |||
67 | public function testShowUser() | ||
68 | { | ||
69 | $client = $this->getClient(); | ||
70 | $em = $client->getContainer()->get('doctrine.orm.entity_manager'); | ||
71 | |||
72 | $this->logInAs('admin'); | ||
73 | |||
74 | /** @var User $user */ | ||
75 | $user = $em->getRepository('WallabagUserBundle:User')->findOneById($this->getLoggedInUserId()); | ||
76 | |||
77 | $user->setName('Bug boss'); | ||
78 | $em->persist($user); | ||
79 | |||
80 | $em->flush(); | ||
81 | |||
82 | $application = new Application($this->getClient()->getKernel()); | ||
83 | $application->add(new ShowUserCommand()); | ||
84 | |||
85 | $command = $application->find('wallabag:user:show'); | ||
86 | |||
87 | $tester = new CommandTester($command); | ||
88 | $tester->execute([ | ||
89 | 'command' => $command->getName(), | ||
90 | 'username' => 'admin', | ||
91 | ]); | ||
92 | |||
93 | $this->assertContains('Display name : Bug boss', $tester->getDisplay()); | ||
94 | } | ||
95 | } | ||