You are here

function Messaging_Methods_Tests::testMessagingMethods in Messaging 6.4

Same name and namespace in other branches
  1. 5 tests/messaging_methods.test \Messaging_Methods_Tests::testMessagingMethods()
  2. 6 tests/messaging_methods.test \Messaging_Methods_Tests::testMessagingMethods()
  3. 6.2 tests/messaging_methods.test \Messaging_Methods_Tests::testMessagingMethods()
  4. 6.3 tests/messaging_methods.test \Messaging_Methods_Tests::testMessagingMethods()
  5. 7 tests/messaging_methods.test \Messaging_Methods_Tests::testMessagingMethods()

Test message sending callbacks for enabled plug-ins

File

tests/messaging_methods.test, line 28

Class

Messaging_Methods_Tests

Code

function testMessagingMethods() {
  $user = $this
    ->drupalCreateUser();
  $user->sms_user[0] = array(
    'status' => 2,
    'number' => 1234,
  );

  // Check sending method properties
  foreach (messaging_method_info() as $method => $info) {

    // Instantiate every send method
    $send_method = messaging_send_method($method, $info);

    // Check for some sending method properties
    $this
      ->assertTrue(!empty($send_method->name) && isset($send_method->type), 'Send method properties seem to be ok for method: ' . $method);

    // Check existing callback functions
    foreach (array(
      'send',
      'user',
      'prepare',
      'render',
      'presend',
      'aftersend',
      'multisend',
    ) as $key) {
      if ($function = _messaging_callback_get($info, $key)) {
        $this
          ->assertTrue(function_exists($function), "Function callback of type {$key} exists for method {$method}");
      }
    }

    // Try preparing and rendering a message for each
    $message = $this
      ->randomMessage();
    $message->method = $method;
    $message
      ->prepare();
    $message
      ->render();
    $this
      ->assertTrue($message->rendered && $message->prepared, 'A message has been prepared and rendered for method: ' . $method);
  }

  // Now we try sending messages with queue = 1, so messages are not actually sent and create user for testing
  foreach (messaging_method_info(NULL, NULL, NULL, TRUE) as $method => $info) {
    $count = 0;
    $name = $info['name'];

    // This should create 3 messages for each method
    $message = $this
      ->randomMessage($method);
    $message->log = 1;

    // All messages will be logged
    if (messaging_user_destination($user, $method, $message)) {
      $this
        ->assertEqual(messaging_message_send_user($user, $message, $method, TRUE), TRUE, 'Message sent for user using: ' . $name);
      $count++;
    }

    // Try these fake destinations, it should work as they're finally send through debug
    $message = $this
      ->randomMessage($method);
    $message->log = 1;

    // All messages will be logged
    $message->account = $user;
    $result = messaging_message_send(array(
      'foo1',
      'foo2',
    ), $message, $method, TRUE);
    $this
      ->assertEqual($result, TRUE, 'Bulk messages sent using: ' . $name);
    $count++;
    $queued = messaging_store()
      ->get_messages(array(
      'uid' => $user->uid,
      'method' => $method,
    ));
    $this
      ->assertEqual(count($queued), $count, 'The right number of messages have been queued for method: ' . $method . ' (Expected=' . $count . ' Real= ' . count($queued) . ')');
  }

  // Create one more user and a few destinations for each sending method
  $user2 = $this
    ->drupalCreateUser();
  $user2->sms_user[0] = array(
    'status' => 2,
    'number' => 3456,
  );
  $users = array(
    $user,
    $user2,
  );
  foreach (messaging_method_info() as $method => $info) {
    $send_method = messaging_send_method($method);
    foreach ($users as $account) {
      $dest = messaging_account_build_destination($account, $method);
      $destinations[$dest->type][$dest->mdid] = $dest;
    }
  }

  // We should have also two destinations of each type
  foreach (messaging_address_info() as $type => $info) {
    $this
      ->assertTableRows('messaging_destination', 2, array(
      'type' => $send_method->address_type,
    ));
    $this
      ->assertEqual(count($destinations[$type]), 2, "We have two destinations of type {$type}");
  }

  // Check deleting destinations and getting them back after method replace
  foreach (array(
    'mail' => 'simple',
    'simple' => 'mail',
  ) as $method => $replace) {
    $type = messaging_send_method($replace)->address_type;
    foreach ($destinations[$type] as $dest) {
      $dest
        ->delete();
    }
    $this
      ->assertTableRows('messaging_destination', 0, array(
      'type' => $type,
    ), "We have successfully deleted destinations of type {$type}");

    // After replacing a mail method by a user one, we should have the destinations again
    messaging_method_disable($method, $replace);
    $old_type = messaging_method_info($method, 'address_type');
    $new_type = messaging_method_info($replace, 'address_type');
    $this
      ->assertTableRows('messaging_destination', 2, array(
      'type' => $type,
    ), "We got back destinations of type {$type} after method replace: from {$method}({$old_type}) to {$replace}({$new_type})");
  }
}