View source
<?php
trait FillPdfTestHelper {
protected $privilegedUser;
protected function createPrivilegedUser() {
drupal_static_reset('checkPermissions');
$this->privilegedUser = $this
->drupalCreateUser(array(
'access administration pages',
'administer pdfs',
'publish all pdfs',
));
$this
->drupalLogin($this->privilegedUser);
}
protected function uploadTestPdf() {
$this
->drupalPost('admin/structure/fillpdf', array(
'files[upload_pdf]' => drupal_realpath(drupal_get_path('module', 'fillpdf') . '/tests/fillpdf_test_v4.pdf'),
), t('Upload'));
$this
->assertText('fillpdf_test_v4.pdf was successfully uploaded.');
$this
->assertResponse(200, 'No integrity constraint violation.');
}
protected function configureBackend() {
variable_set('fillpdf_service', 'test');
variable_set('fillpdf_scheme', 'private');
}
protected function configureLocalServiceBackend() {
variable_set('fillpdf_service', 'local_service');
variable_set('fillpdf_scheme', 'private');
variable_set('fillpdf_local_service_endpoint', 'http://127.0.0.1:8085');
}
protected function configurePdftkBackend() {
variable_set('fillpdf_service', 'pdftk');
variable_set('fillpdf_scheme', 'private');
}
protected function getLatestFillPdfForm() {
return db_query('select MAX(fid) AS fid from {fillpdf_forms};')
->fetchField();
}
public function createImageFieldEntity($image, $field_name, $entity_type, $bundle, $label = NULL) {
$info = entity_get_info($entity_type);
if (empty($image->fid)) {
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;
}
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',
'settings' => array(),
'cardinality' => !empty($field_settings['cardinality']) ? $field_settings['cardinality'] : 1,
);
$field['settings'] = array_merge($field['settings'], $field_settings);
field_create_field($field);
$instance = array(
'field_name' => $field['field_name'],
'entity_type' => $entity_type,
'label' => $name,
'bundle' => $bundle,
'required' => !empty($instance_settings['required']),
'settings' => array(),
'widget' => array(
'type' => 'image_image',
'settings' => array(),
),
);
$instance['settings'] = array_merge($instance['settings'], $instance_settings);
$instance['widget']['settings'] = array_merge($instance['widget']['settings'], $widget_settings);
return field_create_instance($instance);
}
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);
}
}
}