From 344d9edd5860e7b0f44552f5866120e2a1975dff Mon Sep 17 00:00:00 2001
From: babruix <romanalexey@gmail.com>
Date: Thu, 21 Mar 2013 14:20:30 +0100
Subject: [PATCH] Issue #1412964 by xjm: Added tests for Node and User
 actions.

---
 .../lib/Drupal/system/Tests/Actions/NodeTest.php   | 128 +++++++++++++++++++++
 .../lib/Drupal/system/Tests/Actions/UserTest.php   |  42 +++++++
 2 files changed, 170 insertions(+)
 create mode 100644 core/modules/system/lib/Drupal/system/Tests/Actions/NodeTest.php
 create mode 100644 core/modules/system/lib/Drupal/system/Tests/Actions/UserTest.php

diff --git a/core/modules/system/lib/Drupal/system/Tests/Actions/NodeTest.php b/core/modules/system/lib/Drupal/system/Tests/Actions/NodeTest.php
new file mode 100644
index 0000000..438b85a
--- /dev/null
+++ b/core/modules/system/lib/Drupal/system/Tests/Actions/NodeTest.php
@@ -0,0 +1,128 @@
+<?php
+
+/**
+ * @file
+ * Contains \Drupal\system\Tests\Actions\NodeTest.
+ */
+
+namespace Drupal\system\Tests\Actions;
+
+use Drupal\simpletest\WebTestBase;
+
+/**
+ * Test actions defined by node module.
+ */
+class NodeTest extends WebTestBase {
+
+  public static function getInfo() {
+    return array(
+      'name' => 'Actions defined by the Node module',
+      'description' => 'Test coverage for node actions.',
+      'group' => 'Actions',
+    );
+  }
+
+  /**
+   * Test the node publish, sticky, promote and assign owner actions
+   */
+  function testNodePropertiesActions() {
+    $node = $this->_createNode();
+    if (!module_exists('action')) {
+      $module_list = array('action');
+      module_enable($module_list);
+    }
+
+    // Test the publish action.
+    actions_do('node_publish_action', $node);
+    $this->assertEqual($node->status, NODE_PUBLISHED, 'Node reference got updated and published.');
+
+    // Test the unpublish action.
+    actions_do('node_unpublish_action', $node);
+    $this->assertEqual($node->status, NODE_NOT_PUBLISHED, 'Node reference got updated and unpublished.');
+
+    // Test the make sticky action.
+    actions_do('node_make_sticky_action', $node);
+    $this->assertEqual($node->sticky, NODE_STICKY, 'Node reference got updated and made sticky.');
+
+    // Test the make unsticky action.
+    actions_do('node_make_unsticky_action', $node);
+    $this->assertEqual($node->sticky, NODE_NOT_STICKY, 'Node reference got updated and unmade sticky.');
+
+    // Test the promote action.
+    actions_do('node_promote_action', $node);
+    $this->assertEqual($node->promote, NODE_PROMOTED, 'Node reference got updated and promoted.');
+
+    // Test the unpromote action.
+    actions_do('node_unpromote_action', $node);
+    $this->assertEqual($node->promote, NODE_NOT_PROMOTED, 'Node reference got updated and upromoted.');
+
+    // Test the assign owner action.
+    $user = $this->drupalCreateUser(array('administer nodes'));
+    // Ensure that the owner of the node is not the test user.
+    $this->assertNotEqual($node->uid, $user->uid, 'Current owner is different from new one.');
+    // Assign the test user as the owner of the node.
+    actions_do('node_assign_owner_action', $node, array('owner_uid' => $user->uid));
+    $this->assertEqual($node->uid, $user->uid, 'Node reference got updated and owner got changed.');
+  }
+
+  /**
+   * Test the node save action.
+   */
+  function testNodeSaveAction() {
+    $node = $this->_createNode();
+    if (!module_exists('action')) {
+      $module_list = array('action');
+      module_enable($module_list);
+    }
+    // Save the original node status and give it a new one.
+    $old_status = $node->status;
+    $set_status = (int) !$old_status;
+    $node->status = $set_status;
+    $should_be_status = $node->status;
+    // Save the node by triggering the node save action.
+    actions_do('node_save_action', $node);
+
+    // Reload the node to ensure we retrieve the value in the database.
+    $new_node = node_load($node->nid);
+    $new_status = $new_node->status;
+    $this->assertNotEqual($new_status, $old_status, 'Node status was successfully updated in the database.');
+  }
+
+  /**
+   * Test the node unpublish by keyword action.
+   *
+   *  @see theme()
+   */
+  function testNodeUnpublishByKeywordAction() {
+    $node = $this->_createNode();
+    if (!module_exists('action')) {
+      $module_list = array('action');
+      module_enable($module_list);
+    }
+    // Make sure that all modules are loaded before calling theme() function.
+    drupal_container()->get('module_handler')->loadAll();
+    // Publish the node.
+    $node->status = NODE_PUBLISHED;
+    // Generate a pseudo-random keyword based on the title of the node.
+    $keyword = substr($node->title, rand(0, strlen($node->title) / 2), strlen($node->title) / 2);
+
+    // Trigger the node unpublish by keyword action.
+    actions_do('node_unpublish_by_keyword_action', $node, array('keywords' => array($keyword)));
+    $this->assertEqual($node->status, NODE_NOT_PUBLISHED, t('Node reference got unpublished based on the !key keyword.', array('!key' => $keyword)));
+  }
+
+  /**
+   * Create a simple, unpublished, non-sticky and non-promoted node.
+   *
+   * @return Node
+   */
+  function _createNode() {
+    return $this->drupalCreateNode(array(
+      'title' => $this->randomName(8),
+      'promote' => 0,
+      'status' => 0,
+      'sticky' => 0,
+    ));
+  }
+
+}
diff --git a/core/modules/system/lib/Drupal/system/Tests/Actions/UserTest.php b/core/modules/system/lib/Drupal/system/Tests/Actions/UserTest.php
new file mode 100644
index 0000000..6c544f9
--- /dev/null
+++ b/core/modules/system/lib/Drupal/system/Tests/Actions/UserTest.php
@@ -0,0 +1,42 @@
+<?php
+
+/**
+ * @file
+ * Contains \Drupal\system\Tests\Actions\UserTest.
+ */
+
+namespace Drupal\system\Tests\Actions;
+
+use Drupal\simpletest\WebTestBase;
+
+/**
+ * Test actions defined by the user module.
+ */
+class UserTest extends WebTestBase {
+
+  public static function getInfo() {
+    return array(
+      'name' => 'Actions defined by the User module',
+      'description' => 'Test coverage for user actions.',
+      'group' => 'Actions',
+    );
+  }
+
+  /**
+   * Test the user block action.
+   */
+  function testUserBlockAction() {
+    $user = $this->drupalCreateUser(array('access content'));
+    if (!module_exists('action')) {
+      $module_list = array('action');
+      module_enable($module_list);
+    }
+    // Block user by triggering the block user action.
+    actions_do('user_block_user_action', $user);
+
+    // Reload user object from database.
+    $user = user_load($user->uid);
+    $this->assertEqual($user->status, 0, 'User blocked, status is 0 in the database.');
+  }
+
+}
-- 
1.7.12.4 (Apple Git-37)

