You are here

classified_test_notifications.test in Classified Ads 7.3

File

tests/classified_test_notifications.test
View source
<?php

/**
 * Acknowledge breaking PSR1 compliance for test.
 *
 * This is a legacy Simpletest requirement, preventing PSR1 compliance.
 *
 * phpcs:disable PSR1.Files.SideEffects,PSR1.Classes.ClassDeclaration
 */
require_once __DIR__ . '/classified_test_abstract.test';

/**
 * Test cases for the Classified Ads notifications submodule.
 */
class ClassifiedTestNotificationsTest extends ClassifiedTestAbstractTest {

  /**
   * The queue used for notifications.
   *
   * @var \DrupalQueueInterface|null
   */
  public $queue = NULL;

  /**
   * {@inheritdoc}
   */
  public static function getInfo() {
    return array(
      'name' => t('Notifications'),
      'description' => t('Notifications tests for Classified.'),
      'group' => t('Classified'),
    );
  }

  /**
   * Delete and recreate queue.
   */
  public function queueFlush() {
    $this->queue
      ->deleteQueue();
    $this->queue
      ->createQueue('classified_notifications', TRUE);

    // FIXME: use Queue API.
  }

  /**
   * Estimate current number of items in queue.
   *
   * @return mixed
   *   The number of items
   */
  public function queueGetSize() {
    $ret = $this->queue
      ->numberOfItems();
    return $ret;
  }

  /**
   * {@inheritdoc}
   */
  public function setUp() {
    $this->group = __FUNCTION__;
    parent::setUp('classified', 'classified_notifications');
    $this->queue = DrupalQueue::get('classified_notifications', TRUE);
    $this->queue
      ->createQueue();
  }

  /**
   * Issue #1441396.
   *
   * Half-life notification emails continue every notification run.
   *
   * Full test of all notifications.
   */
  public function test1441396() {
    $this->group = 'setup';
    $this
      ->createUsers(array(
      'creator',
    ));

    // 1. Build an ad node expiring in 14 days, grace 7 days (default)
    $settings = array(
      'uid' => $this->creatorUser->uid,
    );
    $node = $this
      ->createNode($settings);
    $lifetime = _classified_get('lifetimes');
    $lifetime = reset($lifetime);
    $grace = _classified_get('grace');
    $this
      ->pass(t('Lifetime %lifetime, grace %grace', array(
      '%lifetime' => $lifetime,
      '%grace' => $grace,
    )), $this->group);
    $t0 = time() + 1;

    // 2. Cron at t0: nothing should happen.
    classified_cron($t0);
    $this
      ->assertFalse($this
      ->queueGetSize(), t('No initial mail queued.'), $this->group);
    $this
      ->queueFlush();

    // 3. Half-life mails.
    $this->group = 'half-life';

    // 3.1 Cron at t0 + 6 days: nothing should happen.
    classified_cron($t0 + ($lifetime / 2 - 1) * 86400);
    $this
      ->assertFalse($this
      ->queueGetSize(), t('No mail before half-life.'), $this->group);
    $this
      ->queueFlush();

    // 3.2 Cron at t0 + 7 days: HL notification should be sent.
    classified_cron($t0 + $lifetime / 2 * 86400);
    $this
      ->assertTrue($this
      ->queueGetSize(), t('Half-life mail queued.'), $this->group);
    $this
      ->queueFlush();
    $node2 = node_load($node->nid, NULL, TRUE);
    $this
      ->assertEqual($node2->status, 1, t('Ad still published at half-life.'), $this->group);

    // 3.3 Cron in the post-half-life period: nothing should happen.
    classified_cron($t0 + $lifetime / 2 * 86400 + 5);
    $this
      ->assertFalse($this
      ->queueGetSize(), t('Half-life mail not requeued 5 minutes later.'), $this->group);
    $this
      ->queueFlush();
    classified_cron($t0 + ($lifetime / 2 + 1) * 86400);
    $this
      ->assertFalse($this
      ->queueGetSize(), t('Half-life mail not requeued 1 day later.'), $this->group);
    $this
      ->queueFlush();
    classified_cron($t0 + ($lifetime / 2 + 1) * 86400);
    $this
      ->assertFalse($this
      ->queueGetSize(), t('Half-life mail not requeued 1 day and 5 minutes later.'), $this->group);
    $this
      ->queueFlush();

    // 4. Expiration mails.
    $this->group = 'expiration';

    // 4.1. Cron at t0 + 13 days: 1 day to expire notification should be sent.
    classified_cron($t0 + ($lifetime - 1) * 86400);
    $this
      ->assertTrue($this
      ->queueGetSize(), t('Pre-expiration mail queued.'), $this->group);
    $this
      ->queueFlush();

    // 4.2. Cron at t0 + 13.5 days: nothing should happen.
    classified_cron($t0 + ($lifetime - 1 + 0.6) * 86400);
    $this
      ->assertFalse($this
      ->queueGetSize(), t('Pre-expiration mail not requeued.'), $this->group);
    $this
      ->queueFlush();

    // 4.3. Cron at t0 + 14 days: expiration notification should be sent, ad
    // should be unpublished.
    classified_cron($t0 + $lifetime * 86400);
    $this
      ->assertTrue($this
      ->queueGetSize(), t('Expiration mail queued.'), $this->group);
    $this
      ->queueFlush();
    $node2 = node_load($node->nid, NULL, TRUE);
    $this
      ->assertEqual($node2->status, 0, t('Ad no longer published after expiration.'), $this->group);

    // 4.4. Cron at t0 + 15 days: nothing should happen.
    classified_cron($t0 + ($lifetime + 1) * 86400);
    $this
      ->assertFalse($this
      ->queueGetSize(), t('Expiration mail not requeued.'), $this->group);
    $this
      ->queueFlush();

    // 5. Purge mails.
    $this->group = 'purge';

    // 5.1 Cron at t0 + 20 days: 1 days to purge notification should be sent.
    classified_cron($t0 + ($lifetime + $grace - 1) * 86400);
    $this
      ->assertTrue($this
      ->queueGetSize(), t('Pre-purge mail queued.'), $this->group);
    $this
      ->queueFlush();

    // 5.2 Cron at t0 + 20.5 days: nothing should happen.
    classified_cron($t0 + ($lifetime + $grace - 1 + 0.6) * 86400);
    $this
      ->assertFalse($this
      ->queueGetSize(), t('Pre-purge mail not requeued.'), $this->group);
    $this
      ->queueFlush();

    // 5.3 Cron at t0 + 21 days: purge notification should be sent, ad should be
    // deleted.
    classified_cron($t0 + ($lifetime + $grace) * 86400);
    $this
      ->assertTrue($this
      ->queueGetSize(), t('Purge mail queued.'), $this->group);
    $this
      ->queueFlush();
    $node2 = node_load($node->nid, NULL, TRUE);
    $this
      ->assertFalse($node2, t('Ad deleted after purge.'), $this->group);

    // 5.4 Cron at t0 + 22 days: ad is deleted, nothing should happen.
    classified_cron($t0 + ($lifetime + $grace + 1) * 86400);
    $this
      ->assertFalse($this
      ->queueGetSize(), t('Purge mail not requeued.'), $this->group);
    $this
      ->queueFlush();
  }

  /**
   * Issue 1491880.
   *
   * Incorrect token use.
   */
  public function test1491880() {
    $this
      ->createUsers(array(
      'creator',
    ));
    $message = array();
    $params = array(
      'account' => $this->creatorUser,
    );
    foreach (_classified_get_notify_kinds() as $kind) {

      // This must not cause notices, as it does in 6.x-3.x-rc2.
      classified_notifications_mail($kind, $message, $params);
    }
  }

}

Classes

Namesort descending Description
ClassifiedTestNotificationsTest Test cases for the Classified Ads notifications submodule.