option to move entries from one entity to another upon deletion (#409)

This commit is contained in:
Kevin Papst
2018-11-12 00:01:49 +01:00
committed by GitHub
parent 1dabeedaa0
commit f9dc42d1f9
18 changed files with 524 additions and 23 deletions

View File

@@ -9,8 +9,10 @@
namespace App\Tests\Controller\Admin;
use App\Entity\Timesheet;
use App\Entity\User;
use App\Tests\Controller\ControllerBaseTest;
use App\Tests\DataFixtures\ActivityFixtures;
use App\Tests\DataFixtures\ProjectFixtures;
use App\Tests\DataFixtures\TimesheetFixtures;
@@ -127,6 +129,14 @@ class ActivityControllerTest extends ControllerBaseTest
$fixture->setAmount(10);
$this->importFixture($em, $fixture);
$timesheets = $em->getRepository(Timesheet::class)->findAll();
$this->assertEquals(10, count($timesheets));
/** @var Timesheet $entry */
foreach($timesheets as $entry) {
$this->assertEquals(1, $entry->getActivity()->getId());
}
$this->request($client, '/admin/activity/1/delete');
$this->assertTrue($client->getResponse()->isSuccessful());
@@ -139,6 +149,60 @@ class ActivityControllerTest extends ControllerBaseTest
$this->assertHasDataTable($client);
$this->assertHasFlashSuccess($client);
// SQLIte does not necessarly support onCascade delete, so these timesheet will stay after deletion
// $em->clear();
// $timesheets = $em->getRepository(Timesheet::class)->findAll();
// $this->assertEquals(0, count($timesheets));
$this->request($client, '/admin/activity/1/edit');
$this->assertFalse($client->getResponse()->isSuccessful());
}
public function testDeleteActionWithTimesheetEntriesAndReplacement()
{
$client = $this->getClientForAuthenticatedUser(User::ROLE_ADMIN);
$em = $client->getContainer()->get('doctrine.orm.entity_manager');
$fixture = new TimesheetFixtures();
$fixture->setUser($this->getUserByRole($em, User::ROLE_USER));
$fixture->setAmount(10);
$this->importFixture($em, $fixture);
$fixture = new ActivityFixtures();
$fixture->setAmount(1)->setIsGlobal(true)->setIsVisible(true);
$this->importFixture($em, $fixture);
$timesheets = $em->getRepository(Timesheet::class)->findAll();
$this->assertEquals(10, count($timesheets));
/** @var Timesheet $entry */
foreach($timesheets as $entry) {
$this->assertEquals(1, $entry->getActivity()->getId());
}
$this->request($client, '/admin/activity/1/delete');
$this->assertTrue($client->getResponse()->isSuccessful());
$form = $client->getCrawler()->filter('form[name=form]')->form();
$this->assertStringEndsWith($this->createUrl('/admin/activity/1/delete'), $form->getUri());
$client->submit($form, [
'form' => [
'activity' => 2
]
]);
$this->assertIsRedirect($client, $this->createUrl('/admin/activity/'));
$client->followRedirect();
$this->assertHasDataTable($client);
$this->assertHasFlashSuccess($client);
$timesheets = $em->getRepository(Timesheet::class)->findAll();
$this->assertEquals(10, count($timesheets));
/** @var Timesheet $entry */
foreach($timesheets as $entry) {
$this->assertEquals(2, $entry->getActivity()->getId());
}
$this->request($client, '/admin/activity/1/edit');
$this->assertFalse($client->getResponse()->isSuccessful());
}

View File

@@ -9,6 +9,7 @@
namespace App\Tests\Controller\Admin;
use App\Entity\Timesheet;
use App\Entity\User;
use App\Tests\Controller\ControllerBaseTest;
use App\Tests\DataFixtures\CustomerFixtures;
@@ -112,6 +113,14 @@ class CustomerControllerTest extends ControllerBaseTest
$fixture->setAmount(10);
$this->importFixture($em, $fixture);
$timesheets = $em->getRepository(Timesheet::class)->findAll();
$this->assertEquals(10, count($timesheets));
/** @var Timesheet $entry */
foreach($timesheets as $entry) {
$this->assertEquals(1, $entry->getActivity()->getId());
}
$this->request($client, '/admin/customer/1/delete');
$this->assertTrue($client->getResponse()->isSuccessful());
@@ -124,6 +133,60 @@ class CustomerControllerTest extends ControllerBaseTest
$this->assertHasDataTable($client);
$this->assertHasFlashSuccess($client);
// SQLIte does not necessarly support onCascade delete, so these timesheet will stay after deletion
// $em->clear();
// $timesheets = $em->getRepository(Timesheet::class)->findAll();
// $this->assertEquals(0, count($timesheets));
$this->request($client, '/admin/customer/1/edit');
$this->assertFalse($client->getResponse()->isSuccessful());
}
public function testDeleteActionWithTimesheetEntriesAndReplacement()
{
$client = $this->getClientForAuthenticatedUser(User::ROLE_ADMIN);
$em = $client->getContainer()->get('doctrine.orm.entity_manager');
$fixture = new TimesheetFixtures();
$fixture->setUser($this->getUserByRole($em, User::ROLE_USER));
$fixture->setAmount(10);
$this->importFixture($em, $fixture);
$fixture = new CustomerFixtures();
$fixture->setAmount(1)->setIsVisible(true);
$this->importFixture($em, $fixture);
$timesheets = $em->getRepository(Timesheet::class)->findAll();
$this->assertEquals(10, count($timesheets));
/** @var Timesheet $entry */
foreach($timesheets as $entry) {
$this->assertEquals(1, $entry->getProject()->getCustomer()->getId());
}
$this->request($client, '/admin/customer/1/delete');
$this->assertTrue($client->getResponse()->isSuccessful());
$form = $client->getCrawler()->filter('form[name=form]')->form();
$this->assertStringEndsWith($this->createUrl('/admin/customer/1/delete'), $form->getUri());
$client->submit($form, [
'form' => [
'customer' => 2
]
]);
$this->assertIsRedirect($client, $this->createUrl('/admin/customer/'));
$client->followRedirect();
$this->assertHasDataTable($client);
$this->assertHasFlashSuccess($client);
$timesheets = $em->getRepository(Timesheet::class)->findAll();
$this->assertEquals(10, count($timesheets));
/** @var Timesheet $entry */
foreach($timesheets as $entry) {
$this->assertEquals(2, $entry->getProject()->getCustomer()->getId());
}
$this->request($client, '/admin/customer/1/edit');
$this->assertFalse($client->getResponse()->isSuccessful());
}

View File

@@ -9,6 +9,7 @@
namespace App\Tests\Controller\Admin;
use App\Entity\Timesheet;
use App\Entity\User;
use App\Tests\Controller\ControllerBaseTest;
use App\Tests\DataFixtures\CustomerFixtures;
@@ -135,6 +136,14 @@ class ProjectControllerTest extends ControllerBaseTest
$fixture->setAmount(10);
$this->importFixture($em, $fixture);
$timesheets = $em->getRepository(Timesheet::class)->findAll();
$this->assertEquals(10, count($timesheets));
/** @var Timesheet $entry */
foreach($timesheets as $entry) {
$this->assertEquals(1, $entry->getActivity()->getId());
}
$this->request($client, '/admin/project/1/delete');
$this->assertTrue($client->getResponse()->isSuccessful());
@@ -147,6 +156,60 @@ class ProjectControllerTest extends ControllerBaseTest
$this->assertHasDataTable($client);
$this->assertHasFlashSuccess($client);
// SQLIte does not necessarly support onCascade delete, so these timesheet will stay after deletion
// $em->clear();
// $timesheets = $em->getRepository(Timesheet::class)->findAll();
// $this->assertEquals(0, count($timesheets));
$this->request($client, '/admin/project/1/edit');
$this->assertFalse($client->getResponse()->isSuccessful());
}
public function testDeleteActionWithTimesheetEntriesAndReplacement()
{
$client = $this->getClientForAuthenticatedUser(User::ROLE_ADMIN);
$em = $client->getContainer()->get('doctrine.orm.entity_manager');
$fixture = new TimesheetFixtures();
$fixture->setUser($this->getUserByRole($em, User::ROLE_USER));
$fixture->setAmount(10);
$this->importFixture($em, $fixture);
$fixture = new ProjectFixtures();
$fixture->setAmount(1)->setIsVisible(true);
$this->importFixture($em, $fixture);
$timesheets = $em->getRepository(Timesheet::class)->findAll();
$this->assertEquals(10, count($timesheets));
/** @var Timesheet $entry */
foreach($timesheets as $entry) {
$this->assertEquals(1, $entry->getProject()->getId());
}
$this->request($client, '/admin/project/1/delete');
$this->assertTrue($client->getResponse()->isSuccessful());
$form = $client->getCrawler()->filter('form[name=form]')->form();
$this->assertStringEndsWith($this->createUrl('/admin/project/1/delete'), $form->getUri());
$client->submit($form, [
'form' => [
'project' => 2
]
]);
$this->assertIsRedirect($client, $this->createUrl('/admin/project/'));
$client->followRedirect();
$this->assertHasDataTable($client);
$this->assertHasFlashSuccess($client);
$timesheets = $em->getRepository(Timesheet::class)->findAll();
$this->assertEquals(10, count($timesheets));
/** @var Timesheet $entry */
foreach($timesheets as $entry) {
$this->assertEquals(2, $entry->getProject()->getId());
}
$this->request($client, '/admin/project/1/edit');
$this->assertFalse($client->getResponse()->isSuccessful());
}

View File

@@ -25,6 +25,14 @@ class ActivityFixtures extends Fixture
* @var int
*/
protected $amount = 0;
/**
* @var bool
*/
protected $isGlobal = false;
/**
* @var bool
*/
protected $isVisible = null;
/**
* @return int
@@ -34,6 +42,28 @@ class ActivityFixtures extends Fixture
return $this->amount;
}
/**
* @param bool $global
* @return $this
*/
public function setIsGlobal(bool $global)
{
$this->isGlobal = $global;
return $this;
}
/**
* @param bool $visible
* @return $this
*/
public function setIsVisible(bool $visible)
{
$this->isVisible = $visible;
return $this;
}
/**
* @param int $amount
* @return ActivityFixtures
@@ -55,10 +85,17 @@ class ActivityFixtures extends Fixture
// random amount of timesheet entries for every user
for ($i = 0; $i < $this->amount; $i++) {
$project = null;
if (false === $this->isGlobal) {
$project = $projects[array_rand($projects)];
}
$visible = 0 != $i % 3;
if (null !== $this->isVisible) {
$visible = $this->isVisible;
}
$entity = new Activity();
$entity
->setProject($projects[array_rand($projects)])
->setProject($project)
->setName($faker->bs . ($visible ? '' : ' (x)'))
->setComment($faker->text)
->setVisible($visible)

View File

@@ -24,6 +24,10 @@ class CustomerFixtures extends Fixture
* @var int
*/
protected $amount = 0;
/**
* @var bool
*/
protected $isVisible = null;
/**
* @return int
@@ -44,6 +48,17 @@ class CustomerFixtures extends Fixture
return $this;
}
/**
* @param bool $visible
* @return $this
*/
public function setIsVisible(bool $visible)
{
$this->isVisible = $visible;
return $this;
}
/**
* {@inheritdoc}
*/
@@ -53,6 +68,9 @@ class CustomerFixtures extends Fixture
for ($i = 0; $i < $this->amount; $i++) {
$visible = 0 != $i % 3;
if (null !== $this->isVisible) {
$visible = $this->isVisible;
}
$entity = new Customer();
$entity
->setCurrency($faker->currencyCode)

View File

@@ -25,6 +25,10 @@ class ProjectFixtures extends Fixture
* @var int
*/
protected $amount = 0;
/**
* @var bool
*/
protected $isVisible = null;
/**
* @return int
@@ -45,6 +49,17 @@ class ProjectFixtures extends Fixture
return $this;
}
/**
* @param bool $visible
* @return $this
*/
public function setIsVisible(bool $visible)
{
$this->isVisible = $visible;
return $this;
}
/**
* {@inheritdoc}
*/
@@ -55,6 +70,9 @@ class ProjectFixtures extends Fixture
for ($i = 0; $i < $this->amount; $i++) {
$visible = 0 != $i % 3;
if (null !== $this->isVisible) {
$visible = $this->isVisible;
}
$entity = new Project();
$entity
->setName($faker->catchPhrase . ($visible ? '' : ' (x)'))