<?php

/**
 * Helper functions for FillPDF testing.
 */
trait FillPdfTestHelper {

  /**
   * The user object for a privileged user.
   *
   * @var object|false
   */
  protected $privilegedUser;

  /**
   * Creates a privileged user account.
   */
  protected function createPrivilegedUser() {
    // Meh. Caching.
    drupal_static_reset('checkPermissions');

    // Create and log in our privileged user.
    $this->privilegedUser = $this->drupalCreateUser(array(
      'access administration pages',
      'administer pdfs',
      'publish all pdfs',
    ));
    $this->drupalLogin($this->privilegedUser);
  }

  /**
   * Uploads a PDF test file.
   */
  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.');
  }

  /**
   * Configures FillPdf using the test backend.
   */
  protected function configureBackend() {
    variable_set('fillpdf_service', 'test');
    variable_set('fillpdf_scheme', 'private');
  }

  /**
   * Configures FillPdf using the local_service backend.
   */
  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');
  }

  /**
   * Configures FillPdf using the pdftk backend.
   */
  protected function configurePdftkBackend() {
    variable_set('fillpdf_service', 'pdftk');
    variable_set('fillpdf_scheme', 'private');
  }

  /**
   * Get the fid of the uploaded file to construct the link.
   *
   * @return int|false
   *   The fid or FALSE if there are no entries in {fillpdf_forms}.
   */
  protected function getLatestFillPdfForm() {
    return db_query('select MAX(fid) AS fid from {fillpdf_forms};')
      ->fetchField();
  }

  /**
   * Create a new image field against a taxonomy term type.
   *
   * @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 array $field_settings
   *   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.
   * @param array $widget_settings
   *   A list of widget settings that will be added to the widget defaults.
   *
   * @return mixed
   *   The return value of field_create_instance().
   *
   * @throws \FieldException
   *
   * @see \ImageFieldTestCase::createImageField()
   */
  protected function createImageFieldForTaxonomyTerm($name, $type_name, $field_settings = array(), $instance_settings = 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' => 'taxonomy_term',
      'label' => $name,
      'bundle' => $type_name,
      '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);
  }

}