/**
* @ORM\ManyToMany(targetEntity="Tag", inversedBy="entries", cascade={"persist"})
- * @ORM\JoinTable
+ * @ORM\JoinTable(
+ * name="entry_tag",
+ * joinColumns={
+ * @ORM\JoinColumn(name="entry_id", referencedColumnName="id")
+ * },
+ * inverseJoinColumns={
+ * @ORM\JoinColumn(name="tag_id", referencedColumnName="id")
+ * }
+ * )
*
* @Groups({"entries_for_user", "export_all"})
*/
}
}
- $this->tags[] = $tag;
+ $this->tags->add($tag);
$tag->addEntry($this);
}
public function removeTag(Tag $tag)
{
+ if (!$this->tags->contains($tag)) {
+ return;
+ }
+
$this->tags->removeElement($tag);
+ $tag->removeEntry($this);
}
/**
return $this->slug;
}
+ /**
+ * @param Entry $entry
+ */
public function addEntry(Entry $entry)
{
- $this->entries[] = $entry;
+ if ($this->entries->contains($entry)) {
+ return;
+ }
+
+ $this->entries->add($entry);
+ $entry->addTag($this);
+ }
+
+ /**
+ * @param Entry $entry
+ */
+ public function removeEntry(Entry $entry)
+ {
+ if (!$this->entries->contains($entry)) {
+ return;
+ }
+
+ $this->entries->removeElement($entry);
+ $entry->removeTag($this);
}
public function hasEntry($entry)
/**
* This test will require an internet connection.
*/
- public function testPostNewThatWillBeTaggued()
+ public function testPostNewThatWillBeTagged()
{
$this->logInAs('admin');
$client = $this->getClient();
$client->submit($form, $data);
$this->assertEquals(302, $client->getResponse()->getStatusCode());
-
- $client->followRedirect();
+ $this->assertContains('/', $client->getResponse()->getTargetUrl());
$em = $client->getContainer()
->get('doctrine.orm.entity_manager');
$em->remove($entry);
$em->flush();
+
+ // and now re-submit it to test the cascade persistence for tags after entry removal
+ // related https://github.com/wallabag/wallabag/issues/2121
+ $crawler = $client->request('GET', '/new');
+
+ $this->assertEquals(200, $client->getResponse()->getStatusCode());
+
+ $form = $crawler->filter('form[name=entry]')->form();
+
+ $data = [
+ 'entry[url]' => $url = 'https://github.com/wallabag/wallabag/tree/master',
+ ];
+
+ $client->submit($form, $data);
+
+ $this->assertEquals(302, $client->getResponse()->getStatusCode());
+ $this->assertContains('/', $client->getResponse()->getTargetUrl());
+
+ $entry = $em
+ ->getRepository('WallabagCoreBundle:Entry')
+ ->findOneByUrl($url);
+
+ $tags = $entry->getTags();
+
+ $this->assertCount(1, $tags);
+ $this->assertEquals('wallabag', $tags[0]->getLabel());
+
+ $em->remove($entry);
+ $em->flush();
}
public function testArchive()