From ba662bad6af3761bed1471f49d635a3269da8d22 Mon Sep 17 00:00:00 2001
From: Bernd Oliver Suenderhauf <bos@suenderhauf.de>
Date: Sat, 6 Apr 2019 19:41:48 +0200
Subject: [PATCH] Issue #3046126 by Pancho: Delete FillPdfFormFields together
 with the FillPdfForm

---
 src/Entity/FillPdfForm.php                    | 19 +++++++++++++
 .../Functional/FillPdfFormDeleteFormTest.php  | 27 ++++++++++++++++++-
 2 files changed, 45 insertions(+), 1 deletion(-)

diff --git a/src/Entity/FillPdfForm.php b/src/Entity/FillPdfForm.php
index 5b6bfdf..968ac07 100644
--- a/src/Entity/FillPdfForm.php
+++ b/src/Entity/FillPdfForm.php
@@ -3,6 +3,7 @@
 namespace Drupal\fillpdf\Entity;
 
 use Drupal\Core\Entity\ContentEntityBase;
+use Drupal\Core\Entity\EntityStorageInterface;
 use Drupal\Core\Entity\EntityTypeInterface;
 use Drupal\Core\Field\BaseFieldDefinition;
 use Drupal\Core\Url;
@@ -153,6 +154,24 @@ class FillPdfForm extends ContentEntityBase implements FillPdfFormInterface {
     return $fields;
   }
 
+  /**
+   * Acts on FillPdfForms before they are deleted and before hooks are invoked.
+   *
+   * Deletes the FillPdfForm's FillPdfFormFields.
+   *
+   * @param \Drupal\Core\Entity\EntityStorageInterface $storage
+   *   The entity storage object.
+   * @param \Drupal\fillpdf\FillPdfFormInterface[] $entities
+   *   An array of FillPdfForms.
+   */
+  public static function preDelete(EntityStorageInterface $storage, array $entities) {
+    parent::preDelete($storage, $entities);
+
+    foreach ($entities as $fillpdf_form) {
+      \Drupal::entityTypeManager()->getStorage('fillpdf_form_field')->delete($fillpdf_form->getFormFields());
+    }
+  }
+
   /**
    * {@inheritdoc}
    */
diff --git a/tests/src/Functional/FillPdfFormDeleteFormTest.php b/tests/src/Functional/FillPdfFormDeleteFormTest.php
index 3ef4457..c756059 100644
--- a/tests/src/Functional/FillPdfFormDeleteFormTest.php
+++ b/tests/src/Functional/FillPdfFormDeleteFormTest.php
@@ -3,6 +3,7 @@
 namespace Drupal\Tests\fillpdf\Functional;
 
 use Drupal\fillpdf\Entity\FillPdfForm;
+use Drupal\fillpdf\Entity\FillPdfFormField;
 use Drupal\Tests\BrowserTestBase;
 use Drupal\Tests\fillpdf\Traits\TestFillPdfTrait;
 use Drupal\Core\Url;
@@ -31,7 +32,7 @@ class FillPdfFormDeleteFormTest extends BrowserTestBase {
   /**
    * Tests the cancel link works.
    */
-  public function testCancelDeletion() {
+  public function testDeleteFormCancel() {
     $this->uploadTestPdf('fillpdf_test_v3.pdf');
     $fillpdf_form = FillPdfForm::load($this->getLatestFillPdfForm());
 
@@ -69,4 +70,28 @@ class FillPdfFormDeleteFormTest extends BrowserTestBase {
     $this->assertSession()->addressEquals($fillpdf_form->toUrl('canonical'));
   }
 
+  /**
+   * Tests the cancel link works.
+   */
+  public function testDeleteForm() {
+    $this->uploadTestPdf('fillpdf_test_v3.pdf');
+    $form_id = $this->getLatestFillPdfForm();
+
+    // Verify the FillPdfForm's fields are stored.
+    $field_ids = \Drupal::entityQuery('fillpdf_form_field')->condition('fillpdf_form', $form_id)->execute();
+    $this->assertCount(3, $field_ids, "3 FillPdfFormFields have been created.");
+
+    // We're on the edit form. Click 'Delete' and confirm deletion.
+    $this->clickLink('Delete');
+    $this->drupalPostForm(NULL, NULL, 'Delete');
+    $this->assertSession()->pageTextContains('FillPDF form deleted.');
+    $this->assertSession()->addressEquals(Url::fromRoute('fillpdf.forms_admin'));
+
+    // Now verify the FillPdfForm and its fields have actually been deleted.
+    $this->assertNull(FillPdfForm::load($form_id), "The FillPdfForm #{$form_id} doesn't exist anymore.");
+    foreach ($field_ids as $id) {
+      $this->assertNull(FillPdfFormField::load($id), "The FillPdfFormField #{$id} doesn't exist anymore.");
+    }
+  }
+
 }
-- 
GitLab