From 4b84289c4aa4efbb7724e2eaf0caecc6dae2d18d Mon Sep 17 00:00:00 2001
From: Bernd Oliver Suenderhauf <bos@suenderhauf.de>
Date: Sun, 17 Mar 2019 16:17:28 +0100
Subject: [PATCH] Issue #3023341 by Pancho: Fix FillPdfFileContext label key;
 add FillPdfForm canonical route

---
 fillpdf.routing.yml                  | 10 ++++
 src/Entity/FillPdfFileContext.php    |  9 ++-
 src/Entity/FillPdfForm.php           |  1 +
 src/Form/FillPdfFormForm.php         |  2 +-
 tests/src/Functional/AdminIdTest.php | 86 ++++++++++++++++++++--------
 5 files changed, 77 insertions(+), 31 deletions(-)

diff --git a/fillpdf.routing.yml b/fillpdf.routing.yml
index 98d4587..85900b6 100644
--- a/fillpdf.routing.yml
+++ b/fillpdf.routing.yml
@@ -26,6 +26,16 @@ fillpdf.populate_pdf:
   requirements:
     _custom_access: '\Drupal\fillpdf\FillPdfAccessController::checkLink'
 
+entity.fillpdf_form.canonical:
+  path: '/admin/structure/fillpdf/{fillpdf_form}'
+  defaults:
+    _entity_form: fillpdf_form.edit
+    _title: 'Edit FillPDF form'
+  requirements:
+    _entity_access: fillpdf_form.update
+  options:
+    _admin_route: TRUE
+
 entity.fillpdf_form.edit_form:
   path: '/admin/structure/fillpdf/{fillpdf_form}'
   defaults:
diff --git a/src/Entity/FillPdfFileContext.php b/src/Entity/FillPdfFileContext.php
index 8d5e62a..eda8a02 100644
--- a/src/Entity/FillPdfFileContext.php
+++ b/src/Entity/FillPdfFileContext.php
@@ -2,8 +2,6 @@
 
 namespace Drupal\fillpdf\Entity;
 
-use Drupal\Core\Annotation\Translation;
-use Drupal\Core\Entity\Annotation\ContentEntityType;
 use Drupal\Core\Entity\EntityStorageInterface;
 use Drupal\Core\Field\BaseFieldDefinition;
 use Drupal\Core\Entity\ContentEntityBase;
@@ -25,20 +23,21 @@ use Drupal\fillpdf\FillPdfFileContextInterface;
  *   base_table = "fillpdf_file_context",
  *   entity_keys = {
  *     "id" = "id",
- *     "label" = "name",
+ *     "label" = "id",
  *     "uuid" = "uuid"
  *   },
  * )
  */
 class FillPdfFileContext extends ContentEntityBase implements FillPdfFileContextInterface {
+
   /**
    * {@inheritdoc}
    */
   public static function preCreate(EntityStorageInterface $storage_controller, array &$values) {
     parent::preCreate($storage_controller, $values);
-    $values += array(
+    $values += [
       'user_id' => \Drupal::currentUser()->id(),
-    );
+    ];
   }
 
   /**
diff --git a/src/Entity/FillPdfForm.php b/src/Entity/FillPdfForm.php
index d2e962d..de33f44 100644
--- a/src/Entity/FillPdfForm.php
+++ b/src/Entity/FillPdfForm.php
@@ -34,6 +34,7 @@ use Drupal\fillpdf\Service\FillPdfAdminFormHelper;
  *     "uuid" = "uuid",
  *   },
  *   links = {
+ *     "canonical" = "/admin/structure/fillpdf/{fillpdf_form}",
  *     "edit-form" = "/admin/structure/fillpdf/{fillpdf_form}",
  *     "delete-form" = "/admin/structure/fillpdf/{fillpdf_form}/delete",
  *     "export-form" = "/admin/structure/fillpdf/{fillpdf_form}/export",
diff --git a/src/Form/FillPdfFormForm.php b/src/Form/FillPdfFormForm.php
index 68ed02b..e1b2568 100644
--- a/src/Form/FillPdfFormForm.php
+++ b/src/Form/FillPdfFormForm.php
@@ -276,7 +276,7 @@ class FillPdfFormForm extends ContentEntityForm {
     $file = $form_state->getValue('upload_pdf');
 
     $message = [];
-    $message[] = $this->t('FillPDF Form %link has been updated.', ['%link' => $entity->toLink(NULL, 'edit-form')->toString()]);
+    $message[] = $this->t('FillPDF Form %link has been updated.', ['%link' => $entity->toLink()->toString()]);
 
     if ($file) {
       $existing_fields = $this->entityHelper->getFormFields($entity);
diff --git a/tests/src/Functional/AdminIdTest.php b/tests/src/Functional/AdminIdTest.php
index df809d3..c488608 100644
--- a/tests/src/Functional/AdminIdTest.php
+++ b/tests/src/Functional/AdminIdTest.php
@@ -2,8 +2,8 @@
 
 namespace Drupal\Tests\fillpdf\Functional;
 
-use Drupal\field\Entity\FieldConfig;
-use Drupal\field\Entity\FieldStorageConfig;
+use Drupal\field\Tests\EntityReference\EntityReferenceTestTrait;
+use Drupal\file\Entity\File;
 use Drupal\fillpdf\Entity\FillPdfForm;
 use Drupal\simpletest\ContentTypeCreationTrait;
 use Drupal\user\Entity\Role;
@@ -19,6 +19,7 @@ use Drupal\Tests\fillpdf\Traits\TestFillPdfTrait;
 class AdminIdTest extends BrowserTestBase {
 
   use ContentTypeCreationTrait;
+  use EntityReferenceTestTrait;
   use TestFileCreationTrait;
   use TestFillPdfTrait;
 
@@ -26,7 +27,7 @@ class AdminIdTest extends BrowserTestBase {
   protected $profile = 'minimal';
 
   /**
-   * @inheritdoc
+   * {@inheritdoc}
    */
   protected function setUp() {
     parent::setUp();
@@ -111,6 +112,42 @@ class AdminIdTest extends BrowserTestBase {
     $this->assertSession()->pageTextContains('Only PDF files are supported, and they must end in .pdf.');
   }
 
+  /**
+   * Tests proper registration of managed_files.
+   */
+  public function testFillPdfFileUsage() {
+    $this->uploadTestPdf('fillpdf_test_v3.pdf');
+
+    // Set the administrative title and check if it has been successfully set.
+    $admin_title = 'Example form';
+    $this->drupalPostForm(NULL, ['admin_title[0][value]' => $admin_title], 'Save');
+    $this->assertSession()->pageTextContains("FillPDF Form $admin_title has been updated.");
+    $this->assertSession()->fieldValueEquals('edit-admin-title-0-value', $admin_title);
+
+    // Grant additional permission to the logged in user.
+    $existing_user_roles = $this->loggedInUser->getRoles(TRUE);
+    $role_to_modify = Role::load(end($existing_user_roles));
+    $this->grantPermissions($role_to_modify, ['access files overview']);
+
+    // Check if the uploaded test PDF file is properly registered as a permanent
+    // managed_file.
+    $fillpdf_form = FillPdfForm::load($this->getLatestFillPdfForm());
+    $file_id = $fillpdf_form->get('file')->first()->getValue()['target_id'];
+    $this->drupalPostForm('admin/content/files', ['edit-filename' => 'fillpdf_test_v3.pdf'], 'Filter');
+    $this->assertSession()->elementsCount('css', 'table td.views-field.views-field-filename', 1);
+    $this->assertSession()->pageTextContains('Permanent');
+    // @todo Past 8.6.x, use File::load($file_id)->createFileUrl() directly.
+    // See https://www.drupal.org/project/fillpdf/issues/3023341.
+    $file_uri = File::load($file_id)->getFileUri();
+    $this->assertSession()->linkByHrefExists(file_create_url($file_uri));
+
+    // Now go check the File usage screen and see if the FillPdfForm is listed
+    // with its canonical link.
+    $this->drupalGet("admin/content/files/usage/$file_id");
+    $this->assertSession()->statusCodeEquals(200);
+    $this->assertSession()->linkByHrefExists($fillpdf_form->toUrl()->toString());
+  }
+
   /**
    * Tests an entity reference to a FillPdfForm.
    */
@@ -129,32 +166,20 @@ class AdminIdTest extends BrowserTestBase {
     $bundle_id = $bundle->id();
 
     // Create an entity reference to our FillPdfForm.
-    $storage = FieldStorageConfig::create([
-      'field_name' => 'field_fillpdf_form',
-      'entity_type' => 'node',
-      'type' => 'entity_reference',
-      'settings' => [
-        'target_type' => 'fillpdf_form',
-      ],
-    ]);
-    $storage->save();
-    $field = FieldConfig::create([
-      'label' => 'FillPDF form',
-      'field_storage' => $storage,
-      'entity_type' => 'node',
-      'bundle' => $bundle_id,
-      'settings' => [
-        'handler' => 'default:fillpdf_form',
-      ],
-    ]);
-    $field->save();
-    \Drupal::entityTypeManager()
+    $this->createEntityReferenceField('node', $bundle_id, 'field_fillpdf_form', 'FillPDF form', 'fillpdf_form');
+    $this->container->get('entity_type.manager')
       ->getStorage('entity_form_display')
       ->load("node.{$bundle_id}.default")
       ->setComponent('field_fillpdf_form', [
         'type' => 'options_select',
-      ])
-      ->save();
+      ])->save();
+    $this->container->get('entity_type.manager')
+      ->getStorage('entity_view_display')
+      ->load("node.{$bundle_id}.default")
+      ->setComponent('field_fillpdf_form', [
+        'type' => 'entity_reference_label',
+        'settings' => ['link' => TRUE],
+      ])->save();
 
     // Grant additional permission to the logged in user.
     $existing_user_roles = $this->loggedInUser->getRoles(TRUE);
@@ -166,6 +191,17 @@ class AdminIdTest extends BrowserTestBase {
     $this->drupalGet("/node/add/{$bundle_id}");
     $this->assertSession()->statusCodeEquals(200);
     $this->assertSession()->optionExists('edit-field-fillpdf-form', $admin_title);
+
+    // Select our FillPdfForm reference, save and see the label is rendered as
+    // canonical link.
+    $edit = [
+      'title[0][value]' => 'Test node',
+      'field_fillpdf_form' => $fid,
+    ];
+    $this->drupalPostForm(NULL, $edit, 'Save');
+    $fillpdf_form = FillPdfForm::load($fid);
+    $this->assertSession()->linkByHrefExists($fillpdf_form->toUrl()->toString());
+
   }
 
 }
-- 
GitLab