]> git.immae.eu Git - github/wallabag/wallabag.git/blame - tests/Wallabag/CoreBundle/Controller/TagControllerTest.php
Add a real configuration for CS-Fixer
[github/wallabag/wallabag.git] / tests / Wallabag / CoreBundle / Controller / TagControllerTest.php
CommitLineData
d0b90fbe
NL
1<?php
2
23634d5d 3namespace Tests\Wallabag\CoreBundle\Controller;
d0b90fbe 4
23634d5d 5use Tests\Wallabag\CoreBundle\WallabagCoreTestCase;
7ab5eb95 6use Wallabag\CoreBundle\Entity\Entry;
ac8cf632 7use Wallabag\CoreBundle\Entity\Tag;
d0b90fbe
NL
8
9class TagControllerTest extends WallabagCoreTestCase
10{
567421af
TC
11 public $tagName = 'opensource';
12
d0b90fbe
NL
13 public function testList()
14 {
15 $this->logInAs('admin');
16 $client = $this->getClient();
17
18 $client->request('GET', '/tag/list');
19
f808b016 20 $this->assertSame(200, $client->getResponse()->getStatusCode());
d0b90fbe 21 }
7244d6cb
NL
22
23 public function testAddTagToEntry()
24 {
25 $this->logInAs('admin');
26 $client = $this->getClient();
27
7ab5eb95 28 $entry = new Entry($this->getLoggedInUser());
29 $entry->setUrl('http://0.0.0.0/foo');
30 $this->getEntityManager()->persist($entry);
31 $this->getEntityManager()->flush();
32 $this->getEntityManager()->clear();
7244d6cb 33
f808b016 34 $crawler = $client->request('GET', '/view/' . $entry->getId());
7244d6cb 35
0d42217e 36 $form = $crawler->filter('form[name=tag]')->form();
7244d6cb 37
4094ea47 38 $data = [
567421af 39 'tag[label]' => $this->tagName,
4094ea47 40 ];
7244d6cb
NL
41
42 $client->submit($form, $data);
f808b016 43 $this->assertSame(302, $client->getResponse()->getStatusCode());
7244d6cb 44
fdc90ceb 45 // be sure to reload the entry
7ab5eb95 46 $entry = $this->getEntityManager()->getRepository(Entry::class)->find($entry->getId());
47 $this->assertCount(1, $entry->getTags());
7244d6cb 48
084fb0d3 49 // tag already exists and already assigned
7244d6cb 50 $client->submit($form, $data);
f808b016 51 $this->assertSame(302, $client->getResponse()->getStatusCode());
7244d6cb 52
7ab5eb95 53 $entry = $this->getEntityManager()->getRepository(Entry::class)->find($entry->getId());
54 $this->assertCount(1, $entry->getTags());
7244d6cb 55
084fb0d3 56 // tag already exists but still not assigned to this entry
4094ea47 57 $data = [
c8de7ab9 58 'tag[label]' => 'foo bar',
4094ea47 59 ];
7244d6cb
NL
60
61 $client->submit($form, $data);
f808b016 62 $this->assertSame(302, $client->getResponse()->getStatusCode());
7244d6cb 63
7ab5eb95 64 $entry = $this->getEntityManager()->getRepository(Entry::class)->find($entry->getId());
65 $this->assertCount(2, $entry->getTags());
7244d6cb 66 }
567421af 67
2baca964
JB
68 public function testAddMultipleTagToEntry()
69 {
70 $this->logInAs('admin');
71 $client = $this->getClient();
72
73 $entry = $client->getContainer()
74 ->get('doctrine.orm.entity_manager')
75 ->getRepository('WallabagCoreBundle:Entry')
74e1f743 76 ->findByUrlAndUserId('http://0.0.0.0/entry2', $this->getLoggedInUserId());
2baca964 77
f808b016 78 $crawler = $client->request('GET', '/view/' . $entry->getId());
2baca964
JB
79
80 $form = $crawler->filter('form[name=tag]')->form();
81
4094ea47 82 $data = [
2baca964 83 'tag[label]' => 'foo2, bar2',
4094ea47 84 ];
2baca964
JB
85
86 $client->submit($form, $data);
f808b016 87 $this->assertSame(302, $client->getResponse()->getStatusCode());
2baca964
JB
88
89 $newEntry = $client->getContainer()
90 ->get('doctrine.orm.entity_manager')
91 ->getRepository('WallabagCoreBundle:Entry')
92 ->find($entry->getId());
93
94 $tags = $newEntry->getTags()->toArray();
74e1f743
JB
95 foreach ($tags as $key => $tag) {
96 $tags[$key] = $tag->getLabel();
97 }
98
2baca964 99 $this->assertGreaterThanOrEqual(2, count($tags));
f808b016
JB
100 $this->assertNotFalse(array_search('foo2', $tags, true), 'Tag foo2 is assigned to the entry');
101 $this->assertNotFalse(array_search('bar2', $tags, true), 'Tag bar2 is assigned to the entry');
2baca964
JB
102 }
103
567421af
TC
104 public function testRemoveTagFromEntry()
105 {
106 $this->logInAs('admin');
107 $client = $this->getClient();
108
7ab5eb95 109 $tag = new Tag();
110 $tag->setLabel($this->tagName);
111 $entry = new Entry($this->getLoggedInUser());
112 $entry->setUrl('http://0.0.0.0/foo');
113 $entry->addTag($tag);
114 $this->getEntityManager()->persist($entry);
115 $this->getEntityManager()->flush();
116 $this->getEntityManager()->clear();
567421af 117
5dbf3f23 118 // We make a first request to set an history and test redirection after tag deletion
f808b016 119 $client->request('GET', '/view/' . $entry->getId());
5dbf3f23 120 $entryUri = $client->getRequest()->getUri();
f808b016 121 $client->request('GET', '/remove-tag/' . $entry->getId() . '/' . $tag->getId());
567421af 122
f808b016
JB
123 $this->assertSame(302, $client->getResponse()->getStatusCode());
124 $this->assertSame($entryUri, $client->getResponse()->getTargetUrl());
567421af 125
6acadf8e 126 // re-retrieve the entry to be sure to get fresh data from database (mostly for tags)
7ab5eb95 127 $entry = $this->getEntityManager()->getRepository(Entry::class)->find($entry->getId());
e686a76d
TC
128 $this->assertNotContains($this->tagName, $entry->getTags());
129
f808b016 130 $client->request('GET', '/remove-tag/' . $entry->getId() . '/' . $tag->getId());
567421af 131
f808b016 132 $this->assertSame(404, $client->getResponse()->getStatusCode());
ac8cf632
JB
133
134 $tag = $client->getContainer()
135 ->get('doctrine.orm.entity_manager')
136 ->getRepository('WallabagCoreBundle:Tag')
137 ->findOneByLabel($this->tagName);
138
f808b016 139 $this->assertNull($tag, $this->tagName . ' was removed because it begun an orphan tag');
567421af 140 }
267e8d63
NL
141
142 public function testShowEntriesForTagAction()
143 {
144 $this->logInAs('admin');
145 $client = $this->getClient();
ac8cf632
JB
146 $em = $client->getContainer()
147 ->get('doctrine.orm.entity_manager');
148
149 $tag = new Tag();
150 $tag->setLabel($this->tagName);
267e8d63
NL
151
152 $entry = $client->getContainer()
153 ->get('doctrine.orm.entity_manager')
154 ->getRepository('WallabagCoreBundle:Entry')
74e1f743 155 ->findByUrlAndUserId('http://0.0.0.0/entry4', $this->getLoggedInUserId());
267e8d63 156
ac8cf632 157 $tag->addEntry($entry);
267e8d63 158
ac8cf632
JB
159 $em->persist($entry);
160 $em->persist($tag);
161 $em->flush();
267e8d63
NL
162
163 $tag = $client->getContainer()
164 ->get('doctrine.orm.entity_manager')
165 ->getRepository('WallabagCoreBundle:Tag')
ac8cf632 166 ->findOneByEntryAndTagLabel($entry, $this->tagName);
267e8d63 167
f808b016 168 $crawler = $client->request('GET', '/tag/list/' . $tag->getSlug());
267e8d63 169
f808b016 170 $this->assertSame(200, $client->getResponse()->getStatusCode());
ac8cf632
JB
171 $this->assertCount(1, $crawler->filter('[id*="entry-"]'));
172
173 $entry->removeTag($tag);
174 $em->remove($tag);
175 $em->flush();
267e8d63 176 }
d0b90fbe 177}