You are here

function MessageShowMessage::testMessageView in Message 7

Test showing a message.

File

tests/message.test, line 71

Class

MessageShowMessage
Test the Message CRUD handling.

Code

function testMessageView() {

  // Add language.
  module_enable(array(
    'locale',
  ));
  require_once DRUPAL_ROOT . '/includes/locale.inc';
  for ($i = 0; $i < 2; ++$i) {
    locale_add_language('l' . $i, $this
      ->randomString(), $this
      ->randomString());
  }
  $property = MESSAGE_FIELD_MESSAGE_TEXT;

  // We use randomName instead of randomString since later-on we use
  // strip_tags, so we don't want to get characters that might be escaped.
  $text1 = $this
    ->randomName() . ' argument -- @foo';
  $text2 = $this
    ->randomName() . ' argument -- @foo';
  $message_type = message_type_create('foo');
  $message_type->{$property} = array(
    'en' => array(
      0 => array(
        'value' => 'english text',
      ),
    ),
    'l0' => array(
      0 => array(
        'value' => $text1,
      ),
    ),
    'l1' => array(
      0 => array(
        'value' => $text2,
      ),
    ),
  );
  $message_type
    ->save();

  // Reload the message type to see it was saved.
  $message_type = message_type_load('foo');
  $this
    ->assertTrue(!empty($message_type->id), t('Message type was saved to the database.'));

  // Assert the message type text field exists and is populated.
  $this
    ->assertEqual($message_type->{$property}['l0'][0]['value'], $text1, t('First language message text was saved to the database.'));
  $this
    ->assertEqual($message_type->{$property}['l1'][0]['value'], $text2, t('Second language message text was saved to the database.'));
  $arguments = array(
    '@foo' => $this
      ->randomName(4),
  );
  $message = message_create('foo', array(
    'arguments' => $arguments,
  ));
  $message
    ->save();

  // Assert the arguments in the message are replaced.
  $output = $message
    ->getText('l0');
  $this
    ->assertEqual(trim(strip_tags($output)), strtr($text1, $arguments), t('Arguments in the first language message were replaced.'));

  // Assert the arguments in the message are replaced when showing a message
  // from another language.
  $output = $message
    ->getText('l1');
  $this
    ->assertEqual(trim(strip_tags($output)), strtr($text2, $arguments), t('Arguments in the second language message were replaced.'));

  // Assert value is of current language when Locale is enabled and langocde
  // is NULL.
  global $language;
  $output = $message
    ->getText(NULL);
  $this
    ->assertEqual(trim(strip_tags($output)), 'english text', 'Assert value is of current language when Locale is enabled and langocde is NULL.');
}