From 2ce2f1af140584d6245c5693fb02320d5f641a3a Mon Sep 17 00:00:00 2001 From: pancho <pancho@15425.no-reply.drupal.org> Date: Wed, 12 Jun 2019 16:51:00 -0400 Subject: [PATCH] Issue #3057804 by Pancho: Replace ImageFieldTestCase by entity-type agnostic test methods --- tests/FillPdfMergeTestCase.test | 73 ++++-------------- tests/FillPdfTestHelper.test | 131 +++++++++++++++++++++++++++++--- 2 files changed, 135 insertions(+), 69 deletions(-) diff --git a/tests/FillPdfMergeTestCase.test b/tests/FillPdfMergeTestCase.test index f630d07..ae4f794 100644 --- a/tests/FillPdfMergeTestCase.test +++ b/tests/FillPdfMergeTestCase.test @@ -3,7 +3,7 @@ /** * Tests that PDF population and token replacement works. */ -class FillPdfMergeTestCase extends ImageFieldTestCase { +class FillPdfMergeTestCase extends DrupalWebTestCase { use FillPdfTestHelper; @@ -27,18 +27,13 @@ class FillPdfMergeTestCase extends ImageFieldTestCase { * {@inheritdoc} */ public function setUp() { - parent::setUp(); - - // ImageFieldTestCase::setUp() is dumb, so we have to enable some modules - // ourselves. $modules = array( 'fillpdf', 'fillpdf_test_webform', 'entity_token', 'fillpdf_test', ); - $success = module_enable($modules, TRUE); - $this->assertTrue($success, t('Enabled modules: %modules', array('%modules' => implode(', ', $modules)))); + parent::setUp($modules); $this->createPrivilegedUser(); $this->configureBackend(); @@ -67,7 +62,7 @@ class FillPdfMergeTestCase extends ImageFieldTestCase { $webform = webform_features_machine_name_load('fillpdf_test_webform'); $this->assertTrue(property_exists($webform, 'nid'), 'Webform properly loaded from Features.'); - $this->createImageField('field_test_image', 'article'); + $this->createImageField('field_test_image', 'node', 'article'); $files = $this->drupalGetTestFiles('image'); $image = reset($files); @@ -79,7 +74,7 @@ class FillPdfMergeTestCase extends ImageFieldTestCase { 'create article content' => TRUE, )); - $this->testNode = node_load($this->uploadNodeImage($image, 'field_test_image', 'article')); + $this->testNode = node_load($this->createImageFieldEntity($image, 'field_test_image', 'node', 'article')); // Test with a node. $this->uploadTestPdf(); @@ -89,7 +84,7 @@ class FillPdfMergeTestCase extends ImageFieldTestCase { // them. $fid = $fillpdf_form->fid; $fields = fillpdf_get_fields($fid); - $this->mapFillPdfFieldsToNodeFields($fields, $fid); + $this->mapFillPdfFieldsToEntityFields('node', $fields, $fid); // Hit the FillPDF URL, check the results from the test fill method. $this->drupalGet('fillpdf', array( @@ -117,7 +112,7 @@ class FillPdfMergeTestCase extends ImageFieldTestCase { // the contributed patch already had those working, and there was no good // reason to take them out and make it harder for people to update their // version of FillPDF later. - $this->testEntity = node_load($this->uploadNodeImage($image, 'field_test_image', 'article')); + $this->testEntity = node_load($this->createImageFieldEntity($image, 'field_test_image', 'node', 'article')); // Test with a node. $this->uploadTestPdf(); @@ -127,7 +122,7 @@ class FillPdfMergeTestCase extends ImageFieldTestCase { // them. $entity_fid = $fillpdf_form_entity->fid; $entity_fields = fillpdf_get_fields($entity_fid); - $this->mapFillPdfFieldsToNodeFields($entity_fields, $entity_fid); + $this->mapFillPdfFieldsToEntityFields('node', $entity_fields, $entity_fid); // Hit the FillPDF URL, check the results from the test fill method. $this->drupalGet('fillpdf', array( @@ -167,19 +162,15 @@ class FillPdfMergeTestCase extends ImageFieldTestCase { and alternative single-entity format).' ); - $this->createImageFieldForTaxonomyTerm('field_term_image', 'tags'); + $this->createImageField('field_term_image', 'taxonomy_term', 'tags'); $term_image = file_save($image); // Test an actual entity token (taxonomy terms). - $test_term = entity_create('taxonomy_term', array( - 'name' => 'Test term', - 'vid' => taxonomy_vocabulary_machine_name_load('tags')->vid, - 'field_term_image' => array(LANGUAGE_NONE => array(0 => (array) $term_image)), - )); - taxonomy_term_save($test_term); + $tid = $this->createImageFieldEntity($term_image, 'field_term_image', 'taxonomy_term', 'tags', 'Test term'); + $test_term = taxonomy_term_load($tid); // Re-map the PDF fields so that the following tests will work. - $this->mapFillPdfFieldsToTaxonomyTermFields($entity_fields, $entity_fid); + $this->mapFillPdfFieldsToEntityFields('taxonomy_term', $entity_fields, $entity_fid); // Hit the FillPDF URL, check the results from the test fill method. $this->drupalGet('fillpdf', array( @@ -323,7 +314,7 @@ class FillPdfMergeTestCase extends ImageFieldTestCase { // them. $entity_access_fid = $entity_access_pdf->fid; $entity_access_fields = fillpdf_get_fields($entity_access_fid); - $this->mapFillPdfFieldsToNodeFields($entity_access_fields, $entity_access_fid); + $this->mapFillPdfFieldsToEntityFields('node', $entity_access_fields, $entity_access_fid); $this->drupalLogin($this->nonPrivilegedUser); $this->drupalGet('fillpdf', array( @@ -571,7 +562,7 @@ class FillPdfMergeTestCase extends ImageFieldTestCase { $this->assertFalse(empty($fields), 'PDF contained actual fields.'); - $this->mapFillPdfFieldsToNodeFields($fields, $fillpdf_form->fid); + $this->mapFillPdfFieldsToEntityFields('node', $fields, $fillpdf_form->fid); // Hit the generation callback, and check the results from merging. $this->drupalGet('fillpdf', array( @@ -585,42 +576,4 @@ class FillPdfMergeTestCase extends ImageFieldTestCase { $this->assertEqual('application/pdf', $finfo->buffer($maybe_pdf)); } - /** - * @param $fields - * @param $fid - */ - protected function mapFillPdfFieldsToNodeFields($fields, $fid) { - foreach ($fields as $pdf_key => $field) { - switch ($pdf_key) { - case 'ImageField': - $field['value'] = '[node:field_test_image]'; - break; - - case 'TextField': - $field['value'] = '[node:title]'; - break; - } - fillpdf_fields_create_update($fid, $pdf_key, $field, TRUE); - } - } - - /** - * @param $fields - * @param $fid - */ - protected function mapFillPdfFieldsToTaxonomyTermFields($fields, $fid) { - foreach ($fields as $pdf_key => $field) { - switch ($pdf_key) { - case 'ImageField': - $field['value'] = '[term:field_term_image]'; - break; - - case 'TextField': - $field['value'] = '[term:name]'; - break; - } - fillpdf_fields_create_update($fid, $pdf_key, $field, TRUE); - } - } - } diff --git a/tests/FillPdfTestHelper.test b/tests/FillPdfTestHelper.test index 2888dd7..c75abe9 100644 --- a/tests/FillPdfTestHelper.test +++ b/tests/FillPdfTestHelper.test @@ -76,18 +76,93 @@ trait FillPdfTestHelper { } /** - * Create a new image field against a taxonomy term type. + * Create an entity with an image. + * + * @param object $image + * A file object representing the image to upload. + * @param string $field_name + * Name of the image field the image should be attached to. + * @param string $entity_type + * The entity's entity type. + * @param string $bundle + * The entity's bundle. + * @param string $label + * (optional) The entity's label. + * + * @return int|false + * Entity ID of the created entity, or FALSE if the entity could not be + * created. + * + * @see ImageFieldTestCase::uploadNodeImage() + */ + public function createImageFieldEntity($image, $field_name, $entity_type, $bundle, $label = NULL) { + $info = entity_get_info($entity_type); + + if (empty($image->fid)) { + // Create a managed file. + file_save($image); + } + + $entity = (object) array( + 'is_new' => TRUE, + 'language' => LANGUAGE_NONE, + ); + if ($bundle_key = $info['entity keys']['bundle']) { + $entity->$bundle_key = $bundle; + } + if ($label_key = $info['entity keys']['label']) { + $entity->$label_key = isset($label) ? $label : $this->randomName(); + } + $entity->$field_name = array( + LANGUAGE_NONE => array( + 0 => array( + 'fid' => $image->fid, + ), + ), + ); + + switch ($entity_type) { + case 'node': + node_save($entity); + return $entity->nid; + + case 'taxonomy_term': + $entity->vid = taxonomy_vocabulary_machine_name_load($bundle)->vid; + $status = taxonomy_term_save($entity); + return $status == SAVED_NEW ? $entity->tid : FALSE; + + case 'user': + $user = user_save($entity); + return $user->uid; + + default: + if (module_exists('entity')) { + entity_save($entity_type, $entity); + $id_key = $info['entity keys']['id']; + return (int) $entity->$id_key; + } + } + + return FALSE; + } + + /** + * Create a new image field. * * @param string $name * The name of the new field (all lowercase), exclude the "field_" prefix. - * @param $type_name - * The node type that this field will be added to. + * @param string $entity_type + * The entity type that this field will be added to. + * @param string $bundle + * The bundle that this field will be added to. * @param array $field_settings - * A list of field settings that will be added to the defaults. + * (optional) A list of field settings that will be added to the defaults. * @param array $instance_settings - * A list of instance settings that will be added to the instance defaults. + * (optional) A list of instance settings that will be added to the instance + * defaults. * @param array $widget_settings - * A list of widget settings that will be added to the widget defaults. + * (optional) A list of widget settings that will be added to the widget + * defaults. * * @return mixed * The return value of field_create_instance(). @@ -96,7 +171,7 @@ trait FillPdfTestHelper { * * @see \ImageFieldTestCase::createImageField() */ - protected function createImageFieldForTaxonomyTerm($name, $type_name, $field_settings = array(), $instance_settings = array(), $widget_settings = array()) { + public function createImageField($name, $entity_type, $bundle, array $field_settings = array(), array $instance_settings = array(), array $widget_settings = array()) { $field = array( 'field_name' => $name, 'type' => 'image', @@ -108,9 +183,9 @@ trait FillPdfTestHelper { $instance = array( 'field_name' => $field['field_name'], - 'entity_type' => 'taxonomy_term', + 'entity_type' => $entity_type, 'label' => $name, - 'bundle' => $type_name, + 'bundle' => $bundle, 'required' => !empty($instance_settings['required']), 'settings' => array(), 'widget' => array( @@ -123,4 +198,42 @@ trait FillPdfTestHelper { return field_create_instance($instance); } + /** + * Maps FillPDF fields to entity fields. + * + * @param string $entity_type + * The entity type. + * @param array $fields + * Associative array of PDF fields. + * @param int $fid + * ID of the FillPDF form. + */ + protected function mapFillPdfFieldsToEntityFields($entity_type, array $fields, $fid) { + $info = entity_get_info($entity_type); + $token_type = $info['token type']; + $label_key = $info['entity keys']['label']; + + $image_field_map = array( + 'node' => '[node:field_test_image]', + 'taxonomy_term' => '[term:field_term_image]', + 'user' => '[user:picture]', + ); + + foreach ($fields as $pdf_key => $field) { + if ($pdf_key == 'ImageField') { + if (isset($image_field_map[$entity_type])) { + $field['value'] = $image_field_map[$entity_type]; + } + } + elseif ($pdf_key == 'TextField') { + $field['value'] .= "[{$token_type}:{$label_key}]"; + } + else { + continue; + } + + fillpdf_fields_create_update($fid, $pdf_key, $field, TRUE); + } + } + } -- GitLab