3 namespace Tests\Wallabag\CoreBundle\Controller
;
5 use Tests\Wallabag\CoreBundle\WallabagCoreTestCase
;
6 use Wallabag\CoreBundle\Entity\Tag
;
8 class TagControllerTest
extends WallabagCoreTestCase
10 public $tagName = 'opensource';
12 public function testList()
14 $this->logInAs('admin');
15 $client = $this->getClient();
17 $client->request('GET', '/tag/list');
19 $this->assertEquals(200, $client->getResponse()->getStatusCode());
22 public function testAddTagToEntry()
24 $this->logInAs('admin');
25 $client = $this->getClient();
27 $entry = $client->getContainer()
28 ->get('doctrine.orm.entity_manager')
29 ->getRepository('WallabagCoreBundle:Entry')
30 ->findByUrlAndUserId('http://0.0.0.0/entry1', $this->getLoggedInUserId());
32 $crawler = $client->request('GET', '/view/'.$entry->getId());
34 $form = $crawler->filter('form[name=tag]')->form();
37 'tag[label]' => $this->tagName
,
40 $client->submit($form, $data);
41 $this->assertEquals(302, $client->getResponse()->getStatusCode());
43 // be sure to reload the entry
44 $entry = $client->getContainer()
45 ->get('doctrine.orm.entity_manager')
46 ->getRepository('WallabagCoreBundle:Entry')
47 ->findByUrlAndUserId('http://0.0.0.0/entry1', $this->getLoggedInUserId());
49 $this->assertEquals(3, count($entry->getTags()));
51 // tag already exists and already assigned
52 $client->submit($form, $data);
53 $this->assertEquals(302, $client->getResponse()->getStatusCode());
55 $newEntry = $client->getContainer()
56 ->get('doctrine.orm.entity_manager')
57 ->getRepository('WallabagCoreBundle:Entry')
58 ->find($entry->getId());
60 $this->assertEquals(3, count($newEntry->getTags()));
62 // tag already exists but still not assigned to this entry
64 'tag[label]' => 'foo',
67 $client->submit($form, $data);
68 $this->assertEquals(302, $client->getResponse()->getStatusCode());
70 $newEntry = $client->getContainer()
71 ->get('doctrine.orm.entity_manager')
72 ->getRepository('WallabagCoreBundle:Entry')
73 ->find($entry->getId());
75 $this->assertEquals(3, count($newEntry->getTags()));
78 public function testAddMultipleTagToEntry()
80 $this->logInAs('admin');
81 $client = $this->getClient();
83 $entry = $client->getContainer()
84 ->get('doctrine.orm.entity_manager')
85 ->getRepository('WallabagCoreBundle:Entry')
86 ->findByUrlAndUserId('http://0.0.0.0/entry2', $this->getLoggedInUserId());
88 $crawler = $client->request('GET', '/view/'.$entry->getId());
90 $form = $crawler->filter('form[name=tag]')->form();
93 'tag[label]' => 'foo2, bar2',
96 $client->submit($form, $data);
97 $this->assertEquals(302, $client->getResponse()->getStatusCode());
99 $newEntry = $client->getContainer()
100 ->get('doctrine.orm.entity_manager')
101 ->getRepository('WallabagCoreBundle:Entry')
102 ->find($entry->getId());
104 $tags = $newEntry->getTags()->toArray();
105 foreach ($tags as $key => $tag) {
106 $tags[$key] = $tag->getLabel();
109 $this->assertGreaterThanOrEqual(2, count($tags));
110 $this->assertNotFalse(array_search('foo2', $tags), 'Tag foo2 is assigned to the entry');
111 $this->assertNotFalse(array_search('bar2', $tags), 'Tag bar2 is assigned to the entry');
114 public function testRemoveTagFromEntry()
116 $this->logInAs('admin');
117 $client = $this->getClient();
119 $entry = $client->getContainer()
120 ->get('doctrine.orm.entity_manager')
121 ->getRepository('WallabagCoreBundle:Entry')
122 ->findByUrlAndUserId('http://0.0.0.0/entry1', $this->getLoggedInUserId());
124 $tag = $client->getContainer()
125 ->get('doctrine.orm.entity_manager')
126 ->getRepository('WallabagCoreBundle:Tag')
127 ->findOneByEntryAndTagLabel($entry, $this->tagName
);
129 $client->request('GET', '/remove-tag/'.$entry->getId().'/'.$tag->getId());
131 $this->assertEquals(302, $client->getResponse()->getStatusCode());
133 $this->assertNotContains($this->tagName
, $entry->getTags());
135 $client->request('GET', '/remove-tag/'.$entry->getId().'/'.$tag->getId());
137 $this->assertEquals(404, $client->getResponse()->getStatusCode());
139 $tag = $client->getContainer()
140 ->get('doctrine.orm.entity_manager')
141 ->getRepository('WallabagCoreBundle:Tag')
142 ->findOneByLabel($this->tagName
);
144 $this->assertNull($tag, $this->tagName
.' was removed because it begun an orphan tag');
147 public function testShowEntriesForTagAction()
149 $this->logInAs('admin');
150 $client = $this->getClient();
151 $em = $client->getContainer()
152 ->get('doctrine.orm.entity_manager');
155 $tag->setLabel($this->tagName
);
157 $entry = $client->getContainer()
158 ->get('doctrine.orm.entity_manager')
159 ->getRepository('WallabagCoreBundle:Entry')
160 ->findByUrlAndUserId('http://0.0.0.0/entry4', $this->getLoggedInUserId());
162 $tag->addEntry($entry);
164 $em->persist($entry);
168 $tag = $client->getContainer()
169 ->get('doctrine.orm.entity_manager')
170 ->getRepository('WallabagCoreBundle:Tag')
171 ->findOneByEntryAndTagLabel($entry, $this->tagName
);
173 $crawler = $client->request('GET', '/tag/list/'.$tag->getSlug());
175 $this->assertEquals(200, $client->getResponse()->getStatusCode());
176 $this->assertCount(1, $crawler->filter('[id*="entry-"]'));
178 $entry->removeTag($tag);