<?php
/**
 * Class for testing messaging module.
 *
 * Tests basic API functions
 */

require_once 'messaging_test_case.inc';

class Messaging_API_Tests extends MessagingTestCase {
  
  function getInfo() {
    return array(
      'name' => 'Messaging API', 
      'group' => 'Messaging', 
      'description' => 'Messaging API functions'
    );
  }

  function setUp() {
    parent::setUp('messaging_simple', 'messaging_mail');
    variable_set('messaging_method_simple', array('filter' => 0));
    variable_set('messaging_method_mail', array('filter' => 0));
  }
  
  /**
   * Exercise basic API functions
   */
  function testMessagingBasicAPI() {
    // Try messaging store functions, build foo method info for rendering
    $message = $this->randomMessage();
    $send_method = messaging_send_method('simple');
    $message->uid = $message->sender = 0;
    $message->method = 'mail';
    $message->queue = 1;    
    $send_method->message_prepare($message);
    $send_method->message_render($message);
    $result = messaging_store()->message_save($message);
    $this->assertTrue($result && $message->mqid, 'The message has been stored successfully');
    $load = messaging_message_load($message->mqid);
    $this->assertTrue($message->mqid == $load->mqid && $message->subject == $load->subject , 'The message has been retrieved successfully');
  
    // Try destination API
    $user = $this->drupalCreateUser();
    $this->assertTrue(Messaging_Destination::validate_method('mail', $user->mail, $user->uid), 'Mail destination for user is OK.');
    $this->assertFalse(Messaging_Destination::validate_method('mail', $user->mail, 0), 'Mail destination is not valid for anonymous user.');
    $destination = Messaging_Destination::create_method('mail', $user->mail, $user->uid);
    $this->assertTrue($destination->mdid, 'A mail destination can be created for user.');
    $destination = Messaging_Destination::get_by_address('mail', $user->mail);
    $this->assertTrue($destination, 'The destination for user can be retrieved.');
    $destination->delete();
    $saved = Messaging_Destination::get_by_address('mail', $user->mail);
    $this->assertFalse($saved, 'The mail destination has been deleted.');

    /**
     * Play with creating, retrieving, deleting a pair messages
     */
    // Clean up the store
    messaging_store()->delete_multiple(array('uid' => 0));
    
    // Create fake method for testing
    $test_method = 'test';
    $test_info = array(
      'glue' => '+', 'subject_glue' => '-', 'footer' => '--', 'filter' => 0,
      'address_type' => 'user', 'log' => 1, 'enabled' => TRUE,
    );
    $send_method = messaging_send_method($test_method, $test_info);

    $user = $this->drupalCreateUser();
    $user->messaging_default = $test_method;
    
    // Check messaging settings API
    $info = messaging_send_method($test_method)->get_info();
    $this->assertEqual(!empty($info), TRUE, 'Messaging method info retrieves information about methods');
    $this->assertEqual($info['filter'] === 0, TRUE, 'Messaging method info retrieves filter information');
    $this->assertEqual(messaging_method_default($user) === $test_method, TRUE, 'Messaging method default is working for test user');

    // Try message composition, a pair simple cases, no filter
    $message = $this->randomMessage($test_method);
    
    // Calculate the rendered body
    $body = implode($info['glue'], array($message->body['header'], $message->body['main'], $info['footer'], $message->body['footer']));
    // The render function now returns a message object
    $render = clone $message;
    $render->render();
    $this->assertEqual(($render->subject == $message->subject && $render->body == $body), TRUE, 'Message successfully rendered, first try');
    
    // Now give it a twist, make subject an array, body a plain text
    $message->subject = array($message->subject, $message->subject);
    $message->body = $body;
    $render = clone $message;
    $render->render();
    $this->assertEqual(($render->subject == implode($info['subject_glue'], $message->subject) && $render->body == $body), TRUE, 'Message successfully rendered, second try');

    // Create fake messages and try sending, they'll end up in messaging queue
    $message1 = $this->randomMessage($test_method);
    $message1->queue = 1;
    $message1->log = 1;
    $message2 = $this->randomMessage($test_method);
    $message2->queue = 1;
    $message2->log = 1;
    $this->assertTrue(messaging_user_destination($user, $test_method), 'Destination can be found for user');
    $this->assertTrue(messaging_message_send_user($user, $message1, NULL, 1), 'Message successfully sent for user');

    $message2->account = $user;
    $this->assertTrue(messaging_message_send(array($user->uid, $user->uid), $message2, $test_method, 1), 'Bulk message successfully sent');
    // Now there should be two messages in queue for this user retrieve using two different methods
    $queued = messaging_store()->get_messages(array('uid' => $user->uid));
    $this->assertEqual(count($queued), 2, 'We have the right number of messages in queue: ' . count($queued));

    // Make messages into logs and then delete
    messaging_store()->message_sent(array_keys($queued), TRUE);
    $logged = messaging_store()->get_messages(array('uid' => $user->uid, 'queue' => 0, 'log' => 1));
    $this->assertEqual(count($logged), 2, 'We have the right number of messages as logs');
    // Try deleting function with many parameters, more than needed actually
    messaging_store()->delete_multiple(array('uid' => $user->uid, 'mqid' => array_keys($queued)));
    $this->assertEqual(count(messaging_store()->get_messages(array('uid' => $user->uid))), 0, 'The logs have been successfully deleted');
    
    // Now try queueing for later sending, we need a push method for that
    $test_method = 'mail';
    $user = $this->drupalCreateUser();
    $user->messaging_default = $test_method;

    $message1 = $this->randomMessage($test_method);
    $message2 = $this->randomMessage($test_method);
    $this->assertTrue(messaging_message_send_user($user, $message1, NULL, 1), 'Message successfully queued for user');
    $this->assertTrue(messaging_message_send(array($user->mail, $user->mail), $message2, $test_method, 1), 'Bulk message successfully queued for multiple destinations.');
    $queued = messaging_store()->get_messages(array('method' => $test_method, 'cron' => 1, 'queue' => 1));
    $this->assertEqual(count($queued), 2, 'We have the right number of messages in queue: ' . count($queued));

    // Process queue and check again
    $results = messaging_store()->queue_process();
    $this->assertEqual(count($results), 2, 'Two rows have been processed.');
    $this->assertEqual(count(array_filter($results)), 2, 'Both rows have been processed successfully.');
    $queued = messaging_store()->get_messages(array('method' => $test_method));
    $this->assertEqual(count($queued), 0, 'We have sent the messages in queue: ' . count($queued));
  }
}
