/**
  * Test node creation by editor.
  *
  * 1. Editor creates Draft node
  * 2. Editor set status from Draft to Final Draft
  * 3. The node appears in the users's overview screen.
  */
 public function testModerateToBeApproved()
 {
     $this->loginAs('editor1');
     $node = $this->drupalCreateNode(array('language' => 'en', 'title' => $this->nodeTitle1, 'type' => 'news', 'workbench_access' => 1007));
     workbench_moderation_moderate($node, 'final_draft');
     $this->loginAs('review_manager1');
     // Set node status To Be Reviewed.
     $options = array('title_field[en][0][value]' => $this->nodeTitle1, 'workbench_moderation_state_new' => 'needs_review');
     $this->drupalPost("node/{$node->nid}/edit", $options, t('Save'));
     entity_get_controller('node')->resetCache(array($node->nid));
     $node = node_load($node->nid);
     $this->assertEquals('needs_review', $node->workbench_moderation['current']->state);
     // Set the reviewer to project_manager1
     $pm1 = user_load_by_name('project_manager1');
     $options = array('project_manager' => $pm1->uid);
     $this->drupalPost("node/{$node->nid}/review", $options, t('Change'));
     $this->drupalGet("node/{$node->nid}/review");
     $this->assertText('project_manager1');
     // Define the list of approvers.
     // Cannot use drupalPost here.
     $ap1 = user_load_by_name('approver1');
     $ap2 = user_load_by_name('approver2 ');
     $form_state = array('node' => $node, 'values' => array('rows' => array($ap1->uid => array('weight' => -10), $ap2->uid => array('weight' => -11))));
     module_load_include('inc', 'osha_workflow', 'osha_workflow.admin');
     drupal_form_submit('osha_workflow_node_approval_form', $form_state, $node);
     $this->drupalGet("node/{$node->nid}/approve");
     $this->assertText($ap1->name);
     $this->assertText($ap2->name);
     $this->loginAs('project_manager1');
     $options = array('workbench_moderation_state_new' => 'to_be_approved');
     $this->drupalPost("node/{$node->nid}/edit", $options, t('Save'));
     entity_get_controller('node')->resetCache(array($node->nid));
     $node = node_load($node->nid);
     $this->assertEquals('to_be_approved', $node->workbench_moderation['current']->state);
 }
Exemple #2
0
 public function afterSave(&$definition)
 {
     if (module_exists('workbench_moderation')) {
         // Update the previous state.
         if (array_key_exists('my_revision', $definition->workbench_moderation)) {
             $definition->workbench_moderation['my_revision']->state = $definition->workbench_moderation['my_revision']->from_state;
         }
         // Update the published VID.
         if (array_key_exists('published', $definition->workbench_moderation)) {
             $definition->workbench_moderation['published']->vid = $definition->vid;
         }
         workbench_moderation_moderate($definition, $definition->workbench_moderation_state_new);
         // Now we need to update the user manually because workbench moderation hard-codes it.
         db_update('workbench_moderation_node_history')->condition('nid', $definition->nid)->condition('vid', $definition->vid)->fields(array('uid' => $definition->uid))->execute();
     }
 }
 public function moderate($wrapper, $fields)
 {
     if (isset($fields['moderation'])) {
         // Make the node author as the revision author.
         // This is needed for workbench views filtering.
         $wrapper->value()->log = $wrapper->value()->uid;
         $wrapper->value()->revision_uid = $wrapper->value()->uid;
         db_update('node_revision')->fields(array('uid' => $wrapper->value()->uid))->condition('nid', $wrapper->getIdentifier(), '=')->execute();
         // Manage moderation state.
         workbench_moderation_moderate($wrapper->value(), $fields["moderation"]);
         // Hack the moderation date
         if (isset($fields['moderation_date'])) {
             $datetime = new \DateTime($fields['moderation_date']);
             db_update('workbench_moderation_node_history')->fields(array('stamp' => $datetime->getTimestamp()))->condition('nid', $wrapper->getIdentifier(), '=')->condition('vid', $wrapper->value()->vid, '=')->execute();
         }
     }
 }
 /**
  * @Given resources:
  */
 public function addResources(TableNode $resourcesTable)
 {
     // Map readable field names to drupal field names.
     $field_map = array('title' => 'title', 'description' => 'body', 'author' => 'uid', 'language' => 'language', 'format' => 'field_format', 'dataset' => 'field_dataset_ref', 'date' => 'created', 'moderation' => 'workbench_moderation');
     // Default to draft moderation state.
     $workbench_moderation_state = 'draft';
     foreach ($resourcesTable as $resourceHash) {
         $node = new stdClass();
         $node->type = 'resource';
         // Defaults
         $node->language = LANGUAGE_NONE;
         foreach ($resourceHash as $key => $value) {
             $drupal_field = $field_map[$key];
             if (!isset($field_map[$key])) {
                 throw new Exception(sprintf("Resource's field %s doesn't exist, or hasn't been mapped. See FeatureContext::addDatasets for mappings.", $key));
             } else {
                 if ($key == 'author') {
                     $user = user_load_by_name($value);
                     if (!isset($user)) {
                         $value = $user->uid;
                     }
                     $drupal_field = $field_map[$key];
                     $node->{$drupal_field} = $value;
                 } elseif ($key == 'format') {
                     $value = $this->explode_list($value);
                     $node->{$drupal_field} = $value;
                 } elseif ($key == 'dataset') {
                     if ($nid = $this->getNidByTitle($value, 'dataset')) {
                         $node->{$drupal_field}['und'][0]['target_id'] = $nid;
                     } else {
                         throw new Exception(sprintf("Dataset node not found."));
                     }
                 } else {
                     if ($key == 'moderation') {
                         // No need to define 'Draft' state as it is used as default.
                         $workbench_moderation_state = $value;
                     } else {
                         // Default behavior.
                         // PHP 5.4 supported notation.
                         $node->{$drupal_field} = $value;
                     }
                 }
             }
         }
         $created_node = $this->getDriver()->createNode($node);
         // Make the node author as the revision author.
         // This is needed for workbench views filtering.
         $created_node->log = $created_node->uid;
         $created_node->revision_uid = $created_node->uid;
         db_update('node_revision')->fields(array('uid' => $created_node->uid))->condition('nid', $created_node->nid, '=')->execute();
         // Manage moderation state.
         workbench_moderation_moderate($created_node, $workbench_moderation_state);
         // Add the created node to the datasets array.
         $this->resources[$created_node->nid] = $created_node;
         // Add the url to the page array for easy navigation.
         $this->addPage(array('title' => $created_node->title, 'url' => '/node/' . $created_node->nid));
     }
 }
 /**
  * @Given I update the moderation state of :named_entity to :state
  * @Given I update the moderation state of :named_entity to :state on date :date
  * @Given :workflow_user updates the moderation state of :named_entity to :state
  * @Given :workflow_user updates the moderation state of :named_entity to :state on date :date
  *
  * Transition a Moderated Node from one state to another.
  *
  * @param String|null $user The string of the username.
  * @param String $named_entity A named entity stored in the entity store.
  * @param String $state The state that you want to transition to.
  * @param String|null $date A valid php datetime string. Supports relative dates.
  * @throws \Exception
  */
 public function transitionModerationState($workflow_user = null, $named_entity, $state, $date = null)
 {
     global $user;
     // Save the original user to set it back later
     $global_user = $user;
     $node = $this->getModerationNode($named_entity);
     $possible_states = workbench_moderation_state_labels();
     $state_key = array_search($state, $possible_states);
     if (!$state_key) {
         $possible_states = implode(", ", $possible_states);
         throw new \Exception("State '{$state}' is not available. All possible states are [{$possible_states}].");
     }
     $current_user = $workflow_user ? user_load_by_name($workflow_user) : $this->getCurrentUser();
     if (!$current_user) {
         throw new \Exception("No user is logged in.");
     }
     $my_revision = $node->workbench_moderation['my_revision'];
     $state_machine_name = array_search($state, $possible_states);
     // If node is moderated to the same state but with different time, then the moderation isn't performed but the time is updated.
     if ($my_revision->state != $state_machine_name) {
         $next_states = workbench_moderation_states_next($my_revision->state, $current_user, $node);
         if (empty($next_states)) {
             $next_states = array();
         }
         if (!isset($next_states[$state_key])) {
             $next_states = implode(", ", $next_states);
             throw new \Exception("State '{$possible_states[$state_key]}' is not available to transition to. Transitions available to user '{$current_user->name}' are [{$next_states}]");
         }
         // Change global user to the current user in order to allow
         // workflow moderation to get the right user.
         $user = $current_user;
         // This function actually updates the transition.
         workbench_moderation_moderate($node, $state_key);
         // the workbench_moderation_moderate defer some status updates on the
         // node (currently the "Publish" status) to the process shutdown. Which
         // does not work well on Behat since scenarios are run on a single drupal
         // bootstrap.
         // To work around this setup. After calling the
         // `workbench_moderation_moderate` callback we check if a call to the
         // `workbench_moderation_store` function is part of the shutdown
         // execution and run it.
         $callbacks =& drupal_register_shutdown_function();
         while (list($key, $callback) = each($callbacks)) {
             if ($callback['callback'] == 'workbench_moderation_store') {
                 call_user_func_array($callback['callback'], $callback['arguments']);
                 unset($callbacks[$key]);
             }
         }
         // Back global user to the original user. Probably an anonymous.
         $user = $global_user;
     }
     // If a specific date is requested, then updated it after the fact.
     if (isset($date)) {
         $timestamp = strtotime($date, REQUEST_TIME);
         if (!$timestamp) {
             throw new \Exception("Error creating datetime from string '{$date}'");
         }
         db_update('workbench_moderation_node_history')->fields(array('stamp' => $timestamp))->condition('nid', $node->nid, '=')->condition('vid', $node->vid, '=')->execute();
     }
 }