<?php
/**
 * @file
 * Test case for testing the cron example module.
 */

/**
 * cron_example test class
 *
 * @ingroup cron_example
 */
class CronExampleTestCase extends DrupalWebTestCase {
  protected $webUser;

  /**
   * {@inheritdoc}
   */
  public static function getInfo() {
    return array(
      'name' => 'Cron example functionality',
      'description' => 'Test the functionality of the Cron Example.',
      'group' => 'Examples',
    );
  }

  /**
   * Enable modules and create user with specific permissions.
   */
  public function setUp() {
    parent::setUp('cron_example');
    // Create user. Search content permission granted for the search block to
    // be shown.
    $this->webUser = $this->drupalCreateUser(array('administer site configuration'));
    $this->drupalLogin($this->webUser);
  }

  /**
   * Test running cron through the user interface.
   */
  public function testCronExampleBasic() {
    // Pretend that cron has never been run (even though simpletest seems to
    // run it once...)
    variable_set('cron_example_next_execution', 0);
    $this->drupalGet('examples/cron_example');

    // Initial run should cause cron_example_cron() to fire.
    $post = array();
    $this->drupalPost('examples/cron_example', $post, t('Run cron now'));
    $this->assertText(t('cron_example executed at'));

    // Forcing should also cause cron_example_cron() to fire.
    $post['cron_reset'] = TRUE;
    $this->drupalPost(NULL, $post, t('Run cron now'));
    $this->assertText(t('cron_example executed at'));

    // But if followed immediately and not forced, it should not fire.
    $post['cron_reset'] = FALSE;
    $this->drupalPost(NULL, $post, t('Run cron now'));
    $this->assertNoText(t('cron_example executed at'));

    $this->assertText(t('There are currently 0 items in queue 1 and 0 items in queue 2'));
    $post = array(
      'num_items' => 5,
      'queue' => 'cron_example_queue_1',
    );
    $this->drupalPost(NULL, $post, t('Add jobs to queue'));
    $this->assertText('There are currently 5 items in queue 1 and 0 items in queue 2');
    $post = array(
      'num_items' => 100,
      'queue' => 'cron_example_queue_2',
    );
    $this->drupalPost(NULL, $post, t('Add jobs to queue'));
    $this->assertText('There are currently 5 items in queue 1 and 100 items in queue 2');

    $post = array();
    $this->drupalPost('examples/cron_example', $post, t('Run cron now'));
    $this->assertPattern('/Queue 1 worker processed item with sequence 5 /');
    $this->assertPattern('/Queue 2 worker processed item with sequence 100 /');
  }
}

/**
 * @} End of "addtogroup cron_example".
 */
