You are here

ExifCustomTestCase.test in EXIF Custom 7

Test all functionality of EXIF Custom

File

tests/ExifCustomTestCase.test
View source
<?php

/**
 * @file
 * Test all functionality of EXIF Custom
 */

/**
 * Test all functionality of EXIF Custom.
 */
class ExifCustomTestCase extends ExifCustomHelper {

  /**
   * {@inheritdoc}
   */
  public static function getInfo() {
    return array(
      'name' => 'EXIF Custom',
      'description' => "Confirm that the module's functionality works.",
      'group' => 'exif_custom',
      'dependencies' => array(
        'file_entity',
      ),
    );
  }

  /**
   * Check the different admin pages.
   */
  public function testSettings() {

    // Load the first admin page.
    $this
      ->drupalGet('admin/config/media/exif_custom');
    $this
      ->assertResponse(200);
    $this
      ->assertText('No mappings have been created yet.');

    // The "New mapping" admin page.
    $this
      ->drupalGet('admin/config/media/exif_custom/add');
    $this
      ->assertResponse(200);
    $this
      ->assertFieldByName('name');
    $this
      ->assertFieldByName('files[example_file]');
    $this
      ->assertFieldByName('op', 'Save');

    // The settings page.
    $this
      ->drupalGet('admin/config/media/exif_custom/settings');
    $this
      ->assertResponse(200);
    $this
      ->assertText('No mappings have been created yet.');
    $this
      ->assertFieldByName('exif_custom_request_method', 'legacy');
    $this
      ->assertFieldByName('exif_custom_request_method', 'new');

    // The user settings page.
    $this
      ->drupalGet('admin/config/media/exif_custom/user');
    $this
      ->assertResponse(200);
    $this
      ->assertText('No mappings have been created yet.');
  }

  /**
   * Run through the module's normal functionality.
   */
  public function testProcess() {
    $module_path = drupal_get_path('module', 'exif_custom');
    $file1 = $module_path . '/tests/test_image1.jpg';
    $file2 = $module_path . '/tests/test_image2.jpg';
    $expected_values = array(
      1 => array(
        'title' => 'Test image title',
        'caption' => 'Test image caption',
        'description' => 'Test image description.',
      ),
      2 => array(
        'title' => 'Test image title 2',
        'caption' => 'Test image caption 2',
        'description' => 'Test image description 2.',
      ),
    );

    // Before anything else, upload an image to confirm how the module works
    // before it's properly configured.
    $this
      ->drupalGet('file/add');
    $this
      ->assertResponse(200);
    $edit = array(
      'files[upload]' => drupal_realpath($file1),
    );
    $this
      ->drupalPost(NULL, $edit, t('Next'));
    if ($this
      ->xpath('//input[@name="scheme"]')) {
      $this
        ->drupalPost(NULL, array(), t('Next'));
    }

    // Load the edit form and confirm the values are empty.
    $this
      ->drupalGet('file/1/edit');
    $this
      ->assertResponse(200);
    $this
      ->assertFieldByName('field_file_image_title_text[und][0][value]', '');
    $this
      ->assertFieldByName('field_file_image_alt_text[und][0][value]', '');

    // Create a mapping. Use the example image for this.
    $this
      ->drupalGet('admin/config/media/exif_custom/add');
    $this
      ->assertResponse(200);
    $edit = array(
      'name' => 'Test mapping',
      'files[example_file]' => drupal_realpath($file1),
    );
    $this
      ->drupalPost(NULL, $edit, 'Save');
    $this
      ->assertResponse(200);

    // Make sure the mapping was saved.
    $this
      ->assertText('New EXIF mapping has been saved: Test mapping');

    // Confirm the expected values were found.
    // EXIF:COMPUTED:Copyright.
    $this
      ->assertText('Creative Commons Attribute-ShareAlike license 2.0');

    // EXIF:COMPUTED:Width and EXIF:EXIF:ExifImageWidth.
    $this
      ->assertText('236');

    // EXIF:COMPUTED:html
    $this
      ->assertText('width="236" height="185"');

    // EXIF:COMPUTED:Height and EXIF:EXIF:ExifImageLength.
    $this
      ->assertText('185');

    // EXIF:EXIF:DateTimeDigitized, EXIF:EXIF:DateTimeOriginal and
    // EXIF:IFD0:DateTime.
    $this
      ->assertText('2009:01:04 12:00:00');

    // EXIF:FILE:FileName.
    $this
      ->assertText('test_image1.jpg');

    // EXIF:FILE:FileSize.
    $this
      ->assertText('17523');

    // EXIF:FILE:MimeType.
    $this
      ->assertText('image/jpeg');

    // EXIF:FILE:SectionsFound.
    $this
      ->assertText('ANY_TAG, IFD0, EXIF');

    // EXIF:IFD0:Artist.
    $this
      ->assertText('Damien McKenna');

    // EXIF:IFD0:Exif_IFD_Pointer.
    $this
      ->assertText('248');

    // EXIF:IFD0:ImageDescription.
    $this
      ->assertText('Test image description.');

    // EXIF:IFD0:XResolution and EXIF:IFD0:YResolution.
    $this
      ->assertText('180/1');

    // IPTC:2#005.
    $this
      ->assertText('Test image title');

    // IPTC:2#025.
    $this
      ->assertText('Test, image, keywords');

    // IPTC:2#120.
    $this
      ->assertText('Test image caption');

    // Update the definition.
    $edit = array(
      // Description.
      'img_field:EXIF:IFD0:ImageDescription' => 'field_file_image_alt_text',
      // Caption.
      'img_field:IPTC:2#005' => 'field_file_image_title_text',
    );
    $this
      ->drupalPost(NULL, $edit, 'Save');
    $this
      ->assertResponse(200);
    $this
      ->assertText('The mapping has been updated.');

    // Set the global mapping.
    $this
      ->drupalGet('admin/config/media/exif_custom/settings');
    $this
      ->assertResponse(200);
    $this
      ->assertNoText('No mappings have been created yet.');
    $this
      ->assertFieldByName('exif_custom_default');
    $this
      ->assertFieldByName('exif_custom_exclude_paths');
    $edit = array(
      'exif_custom_default' => 1,
    );
    $this
      ->drupalPost(NULL, $edit, 'Save configuration');
    $this
      ->assertResponse(200);

    // Make sure the settings saved.
    $this
      ->assertText('The configuration options have been saved.');
    $this
      ->assertFieldByName('exif_custom_default', 1);

    // Upload an image.
    $this
      ->drupalGet('file/add');
    $this
      ->assertResponse(200);
    $edit = array(
      'files[upload]' => drupal_realpath($file2),
    );
    $this
      ->drupalPost(NULL, $edit, t('Next'));
    if ($this
      ->xpath('//input[@name="scheme"]')) {
      $this
        ->drupalPost(NULL, array(), t('Next'));
    }

    // Verify the values were saved correctly.
    $file = $this
      ->getFileByFilename('test_image2.jpg');
    $this
      ->verbose($file);
    $this
      ->assertTrue(isset($file->field_file_image_title_text[LANGUAGE_NONE][0]['value']));
    if (isset($file->field_file_image_title_text[LANGUAGE_NONE][0]['value'])) {
      $this
        ->assertEqual($file->field_file_image_title_text[LANGUAGE_NONE][0]['value'], $expected_values[2]['title']);
    }
    $this
      ->assertTrue(isset($file->field_file_image_alt_text[LANGUAGE_NONE][0]['value']));
    if (isset($file->field_file_image_alt_text[LANGUAGE_NONE][0]['value'])) {
      $this
        ->assertEqual($file->field_file_image_alt_text[LANGUAGE_NONE][0]['value'], $expected_values[2]['description']);
    }

    // Edit the image.
    $this
      ->drupalGet('file/3/edit');
    $this
      ->assertResponse(200);
    $this
      ->assertFieldByName('field_file_image_title_text[und][0][value]', $expected_values[2]['title']);
    $this
      ->assertFieldByName('field_file_image_alt_text[und][0][value]', $expected_values[2]['description']);

    // Change the fields.
    $title_value = 'Title field';
    $alt_value = 'Alt field';
    $edit = array(
      'field_file_image_title_text[und][0][value]' => $title_value,
      'field_file_image_alt_text[und][0][value]' => $alt_value,
    );
    $this
      ->drupalPost(NULL, $edit, 'Save');
    $this
      ->assertResponse(200);

    // Load the image form again and verify the values changed.
    $this
      ->drupalGet('file/3/edit');
    $this
      ->assertResponse(200);
    $this
      ->assertFieldByName('field_file_image_title_text[und][0][value]', $title_value);
    $this
      ->assertFieldByName('field_file_image_alt_text[und][0][value]', $alt_value);

    // Set the global settings to force reloading EXIF data when images are
    // updated.
    $this
      ->drupalGet('admin/config/media/exif_custom/settings');
    $this
      ->assertResponse(200);
    $edit = array(
      'exif_custom_overwrite_existing' => TRUE,
    );
    $this
      ->drupalPost(NULL, $edit, 'Save configuration');
    $this
      ->assertResponse(200);
    $this
      ->assertText('The configuration options have been saved.');

    // Load the image form again just save it as-is.
    $this
      ->drupalGet('file/3/edit');
    $this
      ->assertResponse(200);
    $this
      ->drupalPost(NULL, array(), 'Save');
    $this
      ->assertResponse(200);

    // Load the image form again and confirm that the values are back to the
    // parsed EXIF values.
    $this
      ->drupalGet('file/3/edit');
    $this
      ->assertResponse(200);
    $this
      ->assertFieldByName('field_file_image_title_text[und][0][value]', $expected_values[2]['title']);
    $this
      ->assertFieldByName('field_file_image_alt_text[und][0][value]', $expected_values[2]['description']);
  }

}

Classes

Namesort descending Description
ExifCustomTestCase Test all functionality of EXIF Custom.