home | career | drupal | java | mac | mysql | perl | php | scala | uml | unix

Drupal example source code file (rules_scheduler.test)

This example Drupal source code file (rules_scheduler.test) is included in the DevDaily.com "Drupal Source Code Warehouse" project. The intent of this project is to help you "Learn Drupal by Example".

PHP - Drupal tags/keywords

array, component, date, function, label, node, node1, php, recursion, rule, rules_clear_cache, scheduled, the, title

The rules_scheduler.test Drupal example source code

<?php
// $Id: rules_scheduler.test,v 1.1.2.3 2010/07/29 16:27:18 fago Exp $

/**
 * @file
 * Rules Scheduler tests.
 */

class RulesSchedulerTestCase extends DrupalWebTestCase {

  static function getInfo() {
    return array(
      'name' => 'Rules Scheduler tests',
      'description' => 'Test scheduling components.',
      'group' => 'Rules',
    );
  }

  function setUp() {
    parent::setUp('rules_scheduler');
    RulesLog::logger()->clear();
  }

  /**
   * Tests scheduling components from the action.
   *
   * Note that this also makes sure Rules properly handles timezones, else this
   * test could fail due to a wrong 'now' timestamp.
   */
  function testComponentSchedule() {
    $set = rules_rule_set(array(
      'node1' => array('type' => 'node', 'label' => 'node'),
    ));
    $set->rule(rule()->condition('node_is_published', array('node:select' => 'node1'))
                     ->action('node_unpublish', array('node:select' => 'node1'))
               );
    $set->integrityCheck()->save('rules_test_set_2');
    rules_clear_cache(TRUE);

    // Use different names for the variables to ensure they are properly mapped.
    $rule = rule(array(
      'node2' => array('type' => 'node', 'label' => 'node'),
    ));
    $rule->action('schedule', array(
      'component' => 'rules_test_set_2',
      'identifier' => 'node_[node2:nid]',
      'date' => 'now',
      'param_node1:select' => 'node2',
    ));

    $node = $this->drupalCreateNode(array('title' => 'The title.', 'status' => 1));
    $rule->execute($node);

    // Run cron to let the rules scheduler do its work.
    drupal_cron_run();

    $node = node_load($node->nid, NULL, TRUE);
    $this->assertFalse($node->status, 'The component has been properly scheduled.');
    RulesLog::logger()->checkLog();
  }

  /**
   * Make sure recurion prevention is working fine for scheduled rule sets.
   */
  function testRecursionPrevention() {
    $set = rules_rule_set(array(
      'node1' => array('type' => 'node', 'label' => 'node'),
    ));
    $set->rule(rule()->condition('node_is_published', array('node:select' => 'node1'))
                     ->action('node_unpublish', array('node:select' => 'node1'))
               );
    $set->integrityCheck()->save('rules_test_set_2');

    // Add an reaction rule that is triggered upon a node save. The scheduled
    // component changes the node, thus it would be scheduled again and run in
    // an endless loop.
    $rule = rules_reaction_rule();
    $rule->event('node_insert');
    $rule->event('node_update');
    $rule->action('schedule', array(
      'component' => 'rules_test_set_2',
      'identifier' => '',
      'date' => 'now',
      'param_node1:select' => 'node',
    ));
    $rule->save();
    rules_clear_cache(TRUE);

    // Create a node, what triggers the rule.
    $node = $this->drupalCreateNode(array('title' => 'The title.', 'status' => 1));
    // Run cron to let the rules scheduler do its work.
    drupal_cron_run();

    $node = node_load($node->nid, NULL, TRUE);
    $this->assertFalse($node->status, 'The component has been properly scheduled.');
    $text1 = RulesLog::logger()->render();
    $text2 = RulesTestCase::t('Not evaluating reaction rule %unlabeled to prevent recursion.', array('unlabeled' => $rule->name));
    $this->assertTrue((strpos($text1, $text2) !== FALSE), "Scheduled recursion prevented.");
    RulesLog::logger()->checkLog();
  }
}

Other Drupal examples (source code examples)

Here is a short list of links related to this Drupal rules_scheduler.test source code file:

new blog posts

"Drupal" is a registered trademark of Dries Buytaert.

my drupal tutorials and examples  

Copyright 1998-2016 Alvin Alexander, alvinalexander.com
All Rights Reserved.

Beginning in 2016, a portion of the proceeds from pages under the '/drupal-code-examples/' URI will be donated to charity.