<?php

/**
 * @file
 * Tests for Newsletter module.
 */

/**
 * The NewsletterTemplateTestCase tests CUD actions through Web UI
 * for newsletter templates.
 */
class NewsletterTemplateTestCase extends TaxonomyWebTestCase {
  protected $privileged_user;

  public static function getInfo() {
    return array(
      'name' => 'Newsletter Template',
      'description' => 'Ensure that template CUD is operating properly.',
      'group' => 'Newsletter',
    );
  }

  public function setUp() {
    parent::setUp(array('taxonomy', 'entityreference', 'token', 'newsletter'));
    $this->privileged_user = $this->drupalCreateUser(array(
      'access administration pages',
      'administer newsletters',
      'subscribe newsletters',
      'access newsletter statistics',
    ));
    $this->drupalLogin($this->privileged_user);
  }


  public function testNewsletterTemplateCreate() {
    // Create a vocabulary with a term in it first.
    $vocabulary = $this->createVocabulary();
    $term = $this->createTerm($vocabulary);
    // Create a template.
    $create = array();
    $create['subject'] = $this->randomName(8);
    $create['field_newsletter_body[' . LANGUAGE_NONE . '][0][value]'] = $this->randomString(1000);
    $create['field_' . $vocabulary->machine_name . '[' . LANGUAGE_NONE . '][]'] = $term->tid;
    $this->drupalPost('admin/config/media/newsletter/templates/mail/add', $create, t('Save'));
    $this->assertText(t('Template updated successfully'));
  }

  public function testNewsletterTemplateEdit() {
    // Create a vocabulary with a term in it.
    $vocabulary = $this->createVocabulary();
    $term = $this->createTerm($vocabulary);
    // Edit the Default template.
    $edit = array();
    $edit['subject'] = $this->randomName(8);
    $edit['field_newsletter_body[' . LANGUAGE_NONE . '][0][value]'] = $this->randomString(1000);
    $edit['field_' . $vocabulary->machine_name . '[' . LANGUAGE_NONE . '][]'] = $term->tid;
    $edit['exposed'] = 1;

    $this->drupalPost('admin/config/media/newsletter/templates/edit/4', $edit, t('Update'));
    $this->assertText(t('Template updated successfully'));
  }

  public function testNewsletterTemplateDelete() {
    // Delete the Default template.
    $this->drupalPost('admin/config/media/newsletter/templates/delete/4', NULL, t('Confirm'));
    $this->assertText(t('Template deleted successfully'));
  }

  public function testNewsletterTemplateDeleteMass() {
    // Create some templates.
    for ($i = 5; $i < 10; $i++) {
      $this->testNewsletterTemplateCreate();
      $delete["template[$i]"] = $i;
    }
    $delete['operation'] = 'delete';
    $this->drupalPost('admin/config/media/newsletter/templates', $delete, t('Update'));
    $this->drupalPost(NULL, NULL, t('Confirm'));
    $this->assertText(t('Templates deleted successfully'));
  }

}


/**
 * The NewsletterListTestCase tests CUD actions through Web UI
 * for newsletter lists.
 */
class NewsletterListTestCase extends DrupalWebTestCase {
  protected $privileged_user;

  public static function getInfo() {
    return array(
      'name' => 'Newsletter List',
      'description' => 'Ensure that list CUD is operating properly.',
      'group' => 'Newsletter',
    );
  }

  public function setUp() {
    parent::setUp(array('taxonomy', 'entityreference', 'token', 'newsletter'));
    $this->privileged_user = $this->drupalCreateUser(array(
      'access administration pages',
      'administer newsletters',
      'subscribe newsletters',
      'access newsletter statistics',
    ));
    $this->drupalLogin($this->privileged_user);
  }


  public function testNewsletterListCreate() {
    $create = array();
    $create['title'] = $this->randomName(8);
    $create['field_list_description[' . LANGUAGE_NONE . '][0][value]'] = $this->randomName(15);
    $create['field_newsletter_categories[' . LANGUAGE_NONE . ']'] = $this->randomName(8);
    $create['field_newsletter_template[' . LANGUAGE_NONE . ']'] = 4;
    $create['send_rate'] = 'Daily';

    $this->drupalPost('admin/config/media/newsletter/lists/add', $create, t('Save'));
    $this->assertText(t('List updated successfully'));
  }

  public function testNewsletterListEdit() {
    $edit = array();
    $edit['title'] = $this->randomName(8);
    $edit['field_list_description[' . LANGUAGE_NONE . '][0][value]'] = $this->randomName(15);
    $edit['field_newsletter_categories[' . LANGUAGE_NONE . ']'] = $this->randomName(8);
    $edit['field_newsletter_template[' . LANGUAGE_NONE . ']'] = 4;
    $edit['send_rate'] = 'Custom';
    $edit['send_rate_custom'] = rand(0 , 10);

    $this->drupalPost('admin/config/media/newsletter/lists/edit/1', $edit, t('Update'));
    $this->assertText(t('List updated successfully'));
  }

  public function testNewsletterListDelete() {
    // Delete the Default list.
    $this->drupalPost('admin/config/media/newsletter/lists/delete/1', NULL, t('Confirm'));
    $this->assertText(t('List deleted successfully'));
  }

  public function testNewsletterListDeleteMass() {
    // Create some lists.
    for ($i = 2; $i < 7; $i++) {
      $this->testNewsletterListCreate();
      $delete["list[$i]"] = $i;
    }
    $delete['operation'] = 'delete';
    $this->drupalPost('admin/config/media/newsletter/lists', $delete, t('Update'));
    $this->drupalPost(NULL, NULL, t('Confirm'));
    $this->assertText(t('Lists deleted successfully'));
  }

}
