You are here

public function MailhandlerTestCase::testMailhandlerImport in Mailhandler 7.2

Same name and namespace in other branches
  1. 6.2 tests/mailhandler.test \MailhandlerTestCase::testMailhandlerImport()

Test case.

File

tests/mailhandler.test, line 117
Tests for Mailhandler modules.

Class

MailhandlerTestCase
Mailhandler test cases.

Code

public function testMailhandlerImport() {

  // Create article content type.
  $type = array(
    'type' => 'article',
    'name' => st('Article'),
    'base' => 'node_content',
    'description' => st('Use <em>articles</em> for time-sensitive content like news, press releases or blog posts.'),
    'custom' => 1,
    'modified' => 1,
    'locked' => 0,
  );
  $type = node_type_set_defaults($type);
  node_type_save($type);
  node_add_body_field($type);

  // Create mailboxes.
  $this
    ->createMailboxes();

  // Add field_files to articles.
  $field = array(
    'field_name' => 'field_files',
    'type' => 'file',
    'settings' => array(),
    'cardinality' => 1,
  );
  field_create_field($field);
  $instance = array(
    'field_name' => 'field_files',
    'label' => 'Files',
    'entity_type' => 'node',
    'bundle' => 'article',
    'required' => FALSE,
    'settings' => array(
      'file_extensions' => 'txt png',
    ),
    'widget' => array(
      'type' => 'file_generic',
      'settings' => array(),
    ),
  );
  field_create_instance($instance);

  // Create tags vocabulary and field.
  $description = st('Use tags to group articles on similar topics into categories.');
  $help = st('Enter a comma-separated list of words to describe your content.');
  $vocabulary = (object) array(
    'name' => st('Tags'),
    'description' => $description,
    'machine_name' => 'tags',
    'help' => $help,
  );
  taxonomy_vocabulary_save($vocabulary);
  $this
    ->createTerm('term1');
  $this
    ->createTerm('term2');
  $this
    ->createTerm('term/slash');
  $this
    ->createTerm('term:colon');
  $field = array(
    'field_name' => 'field_' . $vocabulary->machine_name,
    'type' => 'taxonomy_term_reference',
    // Set cardinality to unlimited for tagging.
    'cardinality' => FIELD_CARDINALITY_UNLIMITED,
    'settings' => array(
      'allowed_values' => array(
        array(
          'vocabulary' => $vocabulary->machine_name,
          'parent' => 0,
        ),
      ),
    ),
  );
  field_create_field($field);
  $instance = array(
    'field_name' => 'field_' . $vocabulary->machine_name,
    'entity_type' => 'node',
    'label' => 'Tags',
    'bundle' => 'article',
    'description' => $vocabulary->help,
    'widget' => array(
      'type' => 'taxonomy_autocomplete',
      'weight' => -4,
    ),
    'display' => array(
      'default' => array(
        'type' => 'taxonomy_term_reference_link',
        'weight' => 10,
      ),
      'teaser' => array(
        'type' => 'taxonomy_term_reference_link',
        'weight' => 10,
      ),
    ),
  );
  field_create_instance($instance);

  // Add from fields to articles.
  $fields = array(
    'field_from_address',
    'field_from_name',
    'field_text_test',
  );
  foreach ($fields as $field_name) {
    $field = array(
      'field_name' => $field_name,
      'type' => 'text',
      'settings' => array(),
      'cardinality' => 1,
    );
    field_create_field($field);
    $instance = array(
      'field_name' => $field_name,
      'label' => $field_name,
      'entity_type' => 'node',
      'bundle' => 'article',
      'required' => FALSE,
      'settings' => array(),
      'widget' => array(
        'type' => 'text_textfield',
        'settings' => array(),
      ),
    );
    field_create_instance($instance);
  }

  // Override Mailhandler Default importer, map taxonomy, files, and text fields.
  $this
    ->drupalGet(MAILHANDLER_MENU_PREFIX . '/feeds/mailhandler_nodes/settings/MailhandlerParser');
  $edit = array();
  $edit['available_commands'] = 'status' . "\n" . 'taxonomy' . "\n" . 'Nachname';
  $this
    ->drupalPost(NULL, $edit, t('Save'));
  $this
    ->drupalGet(MAILHANDLER_MENU_PREFIX . '/feeds/mailhandler_nodes/settings/FeedsNodeProcessor');
  $edit = array();
  $edit['bundle'] = 'article';
  $this
    ->drupalPost(NULL, $edit, t('Save'));
  $this
    ->createMapping('body_html', 'body');
  $this
    ->createMapping('taxonomy', 'field_tags');
  $this
    ->createMapping('attachments', 'field_files:uri');
  $this
    ->createMapping('from-address', 'field_from_address');
  $this
    ->createMapping('from-name', 'field_from_name');
  $this
    ->createMapping('Nachname', 'field_text_test');

  // Test import of an empty mailbox.
  $this
    ->createSource('empty');

  // Test import of message - auth user, auth required.
  $this
    ->createSource('test1', 1, TRUE);
  $this
    ->assertTrue($node = $this
    ->drupalGetNodeByTitle('This is a test message 1'));
  $tags = field_get_items('node', $node, 'field_tags', LANGUAGE_NONE);
  $this
    ->assertTrue($node->status);
  $this
    ->assertEqual($tags[0]['tid'], 1);
  $this
    ->assertEqual($tags[1]['tid'], 2);

  // Test that from-address and from-name headers work.
  $from_address = field_get_items('node', $node, 'field_from_address', LANGUAGE_NONE);
  $this
    ->assertEqual($from_address[0]['value'], 'from@example.com');
  $from_name = field_get_items('node', $node, 'field_from_name', LANGUAGE_NONE);
  $this
    ->assertEqual($from_name[0]['value'], 'Dane Powell');
  $edit = array();
  $this
    ->drupalPost('node/' . $node->nid . '/delete', $edit, t('Delete'));

  // Test import of message - auth user, auth not required.
  $this
    ->createSource('test1', 1, FALSE);
  $this
    ->assertTrue($node = $this
    ->drupalGetNodeByTitle('This is a test message 1'));
  $tags = field_get_items('node', $node, 'field_tags', LANGUAGE_NONE);
  $this
    ->assertTrue($node->status);
  $this
    ->assertEqual($tags[0]['tid'], 1);
  $this
    ->assertEqual($tags[1]['tid'], 2);

  // Test import of message - anon user, auth required.
  $this
    ->createSource('test2', 0, TRUE);
  $this
    ->assertFalse($node = $this
    ->drupalGetNodeByTitle('This is a test message 2'));

  // Test import of message - anon user, auth not required.
  $this
    ->createSource('test2', 1, FALSE);
  $this
    ->assertTrue($node = $this
    ->drupalGetNodeByTitle('This is a test message 2'));
  $tags = field_get_items('node', $node, 'field_tags', LANGUAGE_NONE);
  $this
    ->assertFalse($node->status);

  // Test that taxonomy and commands with special characters work.
  $this
    ->assertEqual($tags[0]['tid'], 1);
  $this
    ->assertEqual($tags[1]['tid'], 2);
  $this
    ->assertEqual($tags[2]['tid'], 3);
  $this
    ->assertEqual($tags[3]['tid'], 4);

  // Test import of multiple messages.
  $this
    ->createSource('test3', 2);
  $this
    ->assertTrue($node = $this
    ->drupalGetNodeByTitle('Test message 1 of 2'));
  $this
    ->assertTrue($node = $this
    ->drupalGetNodeByTitle('Test message 2 of 2'));

  // Test import of messages with attachments.
  $this
    ->createSource('attachment1', 1);
  $this
    ->assertTrue($node = $this
    ->drupalGetNodeByTitle('Test attachments 1'));
  $files = field_get_items('node', $node, 'field_files', LANGUAGE_NONE);
  $this
    ->assertEqual($files[0]['filemime'], 'text/plain');
  $this
    ->assertEqual($files[0]['filesize'], 778);

  // Test import of single-part messages.
  $this
    ->createSource('singlepart', 1);
  $this
    ->assertTrue($node = $this
    ->drupalGetNodeByTitle('This is a single-part message'));
  $files = field_get_items('node', $node, 'field_files', LANGUAGE_NONE);
  $this
    ->assertEqual($files[0]['filemime'], 'image/png');

  // Test import of messages with MIME-encoded headers.
  $this
    ->createSource('mime', 1);
  $this
    ->assertTrue($node = $this
    ->drupalGetNodeByTitle('mime header'));

  // Test Multiple Email plugin.
  $this
    ->drupalGet(MAILHANDLER_MENU_PREFIX . '/feeds/mailhandler_nodes/settings/MailhandlerParser');
  $edit = array();
  $edit['authenticate_plugin'] = 'MailhandlerMultipleEmailAuthenticate';
  $this
    ->drupalPost(NULL, $edit, t('Save'));
  $this
    ->createSource('test4', 1, TRUE);
  $this
    ->assertTrue($node = $this
    ->drupalGetNodeByTitle('This is a test message 4'));
  $this
    ->assertEqual($node->uid, $this->mailhandler_user->uid);

  // Test import of messages in ISO-8859 encoding.
  $this
    ->createSource('test5', 1, FALSE);
  $this
    ->assertTrue($node = $this
    ->drupalGetNodeByTitle('Deutscher Verband für Meeresmüll'));

  // Test import of binary content.
  $this
    ->createSource('inline', 1);
  $this
    ->assertTrue($node = $this
    ->drupalGetNodeByTitle('Test inline image'));
  $files = field_get_items('node', $node, 'field_files', LANGUAGE_NONE);
  $filename_actual = drupal_realpath('public://') . '/../../' . $files[0]['filename'];
  $filename_actual1 = drupal_realpath('public://') . '/' . $files[0]['filename'];
  if (file_exists($filename_actual1)) {
    $filename_actual = $filename_actual1;
  }
  $handle_actual = fopen($filename_actual, 'r');
  $filestring_actual = fread($handle_actual, filesize($filename_actual));
  $filename_expected = drupal_get_path('module', 'mailhandler') . '/tests/druplicon.png';
  $handle_expected = fopen($filename_expected, 'r');
  $filestring_expected = fread($handle_expected, filesize($filename_expected));
  $this
    ->assertTrue($filestring_expected === $filestring_actual);

  // Test that URLs in the first line of an email don't get stripped (interpreted as command).
  $this
    ->createSource('firstline_url', 1, FALSE);
  $this
    ->assertTrue($node = $this
    ->drupalGetNodeByTitle('Test firstline URLS'));
  $this
    ->drupalGet('node/' . $node->nid);
  $this
    ->assertLinkByHref('http://example.com');

  // Test that capitalized commands work.
  $this
    ->createSource('capital_commands', 1, FALSE);
  $this
    ->assertTrue($node = $this
    ->drupalGetNodeByTitle('Test capital commands'));
  $text_test = field_get_items('node', $node, 'field_text_test', LANGUAGE_NONE);
  $this
    ->assertEqual($text_test[0]['value'], 'testing');
}