You are here

sharemessage.test in Share Message 7

ShareMessage tests.

File

tests/sharemessage.test
View source
<?php

/**
 * @file
 * ShareMessage tests.
 */
class ShareMessageTestCase extends DrupalWebTestCase {
  public function setUp() {
    parent::setUp('sharemessage', 'sharemessage_test');

    // Create an admin user.
    $permissions = array(
      'access administration pages',
      'administer blocks',
      'administer sharemessage entities',
      'view sharemessage entities',
      'administer site configuration',
    );
    $admin_user = $this
      ->drupalCreateUser($permissions);
    $this
      ->drupalLogin($admin_user);

    // Add profile ID to the global settings.
    variable_set('sharemessage_addthis_profile_id', 'ra-5006849061326d1cl');
  }

}
class ShareMessageWorkflowTestCase extends ShareMessageTestCase {
  public static function getInfo() {
    return array(
      'name' => 'ShareMessage workflow',
      'description' => 'Main sharemessage workflow through the admin UI.',
      'group' => 'ShareMessage',
    );
  }

  /**
   * Main sharemessage workflow through the admin UI.
   */
  public function testShareMessageWorkflow() {

    // Step 1: Create a share message in the UI.
    $this
      ->drupalGet('admin/config/services/sharemessage/add');
    $edit = array(
      'label' => 'ShareMessage Test Label',
      'name' => 'sharemessage_test_label',
      'sharemessage_title[en][0][value]' => 'ShareMessage Test Title',
      'sharemessage_long[en][0][value]' => 'ShareMessage Test Long Description',
      'sharemessage_short[en][0][value]' => 'ShareMessage Test Short Description',
      'sharemessage_image_url[und][0][value]' => 'http://www.example.com/drupal.jpg',
      'sharemessage_url[und][0][value]' => 'http://www.example.com',
    );
    $this
      ->drupalPost(NULL, $edit, t('Save share message'));
    $this
      ->assertText(t('Message @label saved.', array(
      '@label' => $edit['label'],
    )), t('ShareMessage is successfully saved.'));

    // Step 2: Display share message and verify AddThis markup
    // and meta header elements.
    $this
      ->drupalGet('sharemessage-test/1');
    $raw_html_string = '<div class="addthis_toolbox addthis_default_style addthis_16x16_style"';
    $this
      ->assertRaw($raw_html_string, t('AddThis buttons are displayed.'));
    $meta_title = '<meta property="og:title" content="' . $edit['sharemessage_title[en][0][value]'] . '" />';
    $this
      ->assertRaw($meta_title, t('OG:title exists and has appropriate content.'));
    $meta_description = '<meta property="og:description" content="' . $edit['sharemessage_long[en][0][value]'] . '" />';
    $this
      ->assertRaw($meta_description, t('OG:description exists and has appropriate content.'));
    $meta_image = '<meta property="og:image" content="' . $edit['sharemessage_image_url[und][0][value]'] . '" />';
    $this
      ->assertRaw($meta_image, t('OG:image exists and has appropriate content.'));
    $meta_url = '<meta property="og:url" content="' . $edit['sharemessage_url[und][0][value]'] . '" />';
    $this
      ->assertRaw($meta_url, t('OG:url exists and has appropriate content.'));
    $this
      ->drupalGet('admin/config/services/sharemessage/add');

    // Check if the enforce checkbox is there.
    $this
      ->assertFieldByName('enforce_usage', NULL, 'The enforce checkbox was found.');
    $edit_2 = array(
      'label' => 'ShareMessage 2 Test Label',
      'name' => 'sharemessage_test_label2',
      'sharemessage_title[en][0][value]' => 'ShareMessage 2 Test Title',
      'sharemessage_long[en][0][value]' => 'ShareMessage 2 Test Long Description',
      'sharemessage_short[en][0][value]' => 'ShareMessage 2 Test Short Description',
      'sharemessage_image_url[und][0][value]' => $edit['sharemessage_image_url[und][0][value]'],
      'sharemessage_url[und][0][value]' => $edit['sharemessage_url[und][0][value]'],
      'enforce_usage' => 1,
    );
    $this
      ->drupalPost(NULL, $edit_2, t('Save share message'));
    $sharemessage = entity_load_single('sharemessage', 2);

    // Check if the option was saved as expected.
    $this
      ->assertEqual(!empty($sharemessage->settings['enforce_usage']), TRUE, 'Enforce setting was saved on the entity.');
    $this
      ->drupalGet('sharemessage-test/1', array(
      'query' => array(
        'smid' => 2,
      ),
    ));

    // Check if the og:description tag gets rendered correctly.
    $meta_description = '<meta property="og:description" content="' . $edit_2['sharemessage_long[en][0][value]'] . '" />';
    $this
      ->assertRaw($meta_description, t('OG:description was overridden properly.'));

    // Check if the og:url tag gets rendered correctly.
    $url = url($edit['sharemessage_url[und][0][value]'], array(
      'query' => array(
        'smid' => 2,
      ),
    ));
    $meta_url = '<meta property="og:url" content="' . $url . '" />';
    $this
      ->assertRaw($meta_url, t('OG:url has correct query string.'));

    // Disable enforcement of overrides in the global settings.
    variable_set('sharemessage_message_enforcement', FALSE);
    $this
      ->drupalGet('sharemessage-test/1', array(
      'query' => array(
        'smid' => 2,
      ),
    ));

    // Check if the og:description tag gets rendered correctly.
    $meta_description = '<meta property="og:description" content="' . $edit['sharemessage_long[en][0][value]'] . '" />';
    $this
      ->assertRaw($meta_description, t('OG:description was not overridden.'));

    // Check if the og:url tag gets rendered correctly.
    $meta_url = '<meta property="og:url" content="' . $edit['sharemessage_url[und][0][value]'] . '" />';
    $this
      ->assertRaw($meta_url, t('OG:url does not contain query string.'));
  }

}
class ShareMessageExposeToBlockTestCase extends ShareMessageTestCase {
  public static function getInfo() {
    return array(
      'name' => 'ShareMessage blocks',
      'description' => 'Check if sharemessage is exposed as block.',
      'group' => 'ShareMessage',
    );
  }

  /**
   * Test case that check if sharemessage is exposed as block.
   */
  public function testShareMessageExposeToBlock() {

    // Step 1: Create another sharemessage that will be exposed as block.
    $this
      ->drupalGet('admin/config/services/sharemessage/add');
    $sharemessage = array(
      'label' => 'ShareMessage Test Label',
      'name' => 'sharemessage_test_label',
      'block' => 1,
    );
    $this
      ->drupalPost(NULL, $sharemessage, t('Save share message'));
    $this
      ->assertText(t('Message @label saved.', array(
      '@label' => $sharemessage['label'],
    )));

    // Step 2: Go to block section and enable block to be visible
    // on content region.
    $this
      ->drupalGet('admin/structure/block');
    $this
      ->assertRaw($sharemessage['label'], t($sharemessage['label'] . ' block exists.'));
    $edit = array(
      'blocks[sharemessage_' . $sharemessage['name'] . '][region]' => 'content',
    );
    $this
      ->drupalPost('admin/structure/block', $edit, t('Save blocks'));

    // Step 3: Go to fron page and check if sharemessage is shown.
    $this
      ->drupalGet('<front>');
    $raw_html_string = '<div class="addthis_toolbox addthis_default_style addthis_16x16_style"';
    $this
      ->assertRaw($raw_html_string, t('AddThis buttons are displayed as block on homepage.'));
  }

}
class ShareMessageSettingsTestCase extends ShareMessageTestCase {
  public static function getInfo() {
    return array(
      'name' => 'ShareMessage settings',
      'description' => 'Check if default and sharemessage specific settings work correctly.',
      'group' => 'ShareMessage',
    );
  }

  /**
   * Test case that check if default and sharemessage specific settings work correctly.
   */
  public function testShareMessageSettings() {

    // Step 1: Setup default settings.
    $this
      ->drupalGet('admin/config/services/sharemessage/settings');
    $default_settings = array(
      'sharemessage_default_services[]' => array(
        'facebook',
        'facebook_like',
      ),
      'sharemessage_default_additional_services' => FALSE,
      'sharemessage_default_icon_style' => 'addthis_16x16_style',
    );
    $this
      ->drupalPost(NULL, $default_settings, t('Save configuration'));

    // Step 2: Create share message with customized settings.
    $this
      ->drupalGet('admin/config/services/sharemessage/add');
    $sharemessage = array(
      'label' => 'ShareMessage Test Label',
      'name' => 'sharemessage_test_label',
      'override_default_settings' => 1,
      'settings[services][]' => array(
        'facebook',
      ),
      'settings[additional_services]' => 1,
      'settings[icon_style]' => 'addthis_32x32_style',
    );
    $this
      ->drupalPost(NULL, $sharemessage, t('Save share message'));
    $this
      ->assertText(t('Message @label saved.', array(
      '@label' => $sharemessage['label'],
    )));

    // Step 3: Verify that settings are overridden
    // (services, additional_services and icon_style).
    $this
      ->drupalGet('sharemessage-test/1');
    $raw_html_additional_services = '<a class="addthis_button_compact"></a>';
    $raw_html_icon_style = '<div class="addthis_toolbox addthis_default_style ' . $sharemessage['settings[icon_style]'] . '"';

    // Check services (facebook_like button should not be displayed).
    $facebook_button = $this
      ->xpath('//a[contains(@class,"addthis_button_facebook_like")]');
    $this
      ->assertEqual(empty($facebook_button), TRUE, t('Facebook like button that is globally enabled is not displayed on the page, so that the global settings are overridden.'));

    // Additional services should be displayed.
    $this
      ->assertRaw($raw_html_additional_services, t('Additional services button is displayed, so that the global settings are overridden.'));

    // Check icon style.
    $this
      ->assertRaw($raw_html_icon_style, t('Icon style is changed to "' . $sharemessage['settings[icon_style]'] . '" so that the global settings are overridden.'));

    // Step 4: Uncheck "Override default settings" checkbox.
    $this
      ->drupalGet('admin/config/services/sharemessage/manage/' . $sharemessage['name']);
    $edit = array(
      'override_default_settings' => FALSE,
    );
    $this
      ->drupalPost(NULL, $edit, t('Save share message'));
    $this
      ->assertText(t('Message @label saved.', array(
      '@label' => $sharemessage['label'],
    )));

    // Step 5: Check that addThis widget is displayed with default settings.
    $this
      ->drupalGet('sharemessage-test/1');
    $facebook_button = $this
      ->xpath('//a[contains(@class,"addthis_button_facebook_like")]');

    // Check services (facebook_like button should be displayed).
    $this
      ->assertTrue(!empty($facebook_button), t('Facebook like button is displayed as it is globally configured.'));

    // Additional services button should not be displayed.
    $this
      ->assertNoRaw($raw_html_additional_services, t('Additional services buttion is not displayed as it is globally configured.'));

    // Check icon style (should be addthis_16x16_style).
    $raw_html_default_icon_style = '<div class="addthis_toolbox addthis_default_style ' . $default_settings['sharemessage_default_icon_style'] . '"';
    $this
      ->assertRaw($raw_html_default_icon_style, t('Default icon style is used.'));
  }

}
class ShareMessageViewModesTest extends ShareMessageTestCase {
  public static function getInfo() {
    return array(
      'name' => 'ShareMessage view modes',
      'description' => 'Verifies the output of ShareMessage entities different view modes.',
      'group' => 'ShareMessage',
    );
  }

  /**
   * Checks whether view modes render expected markup.
   */
  public function testShareMessageViewModes() {
    $edit = array(
      'label' => 'ShareMessage Test Label',
      'name' => 'sharemessage_test_label',
      'sharemessage_title[en][0][value]' => 'ShareMessage Test Title',
      'sharemessage_long[en][0][value]' => 'ShareMessage Test Long Description',
      'sharemessage_short[en][0][value]' => 'ShareMessage Test Short Description',
      'sharemessage_image_url[und][0][value]' => 'http://www.example.com/drupal.jpg',
      'sharemessage_url[und][0][value]' => 'http://www.example.com',
    );
    $this
      ->drupalPost('admin/config/services/sharemessage/add', $edit, t('Save share message'));
    $this
      ->assertText(t('Message @label saved.', array(
      '@label' => $edit['label'],
    )), t('ShareMessage is successfully saved.'));
    $this
      ->drupalGet('sharemessage-test/1');
    $this
      ->assertShareButtons($edit, TRUE);
    $this
      ->assertOGTags($edit['sharemessage_title[en][0][value]']);
    variable_set('sharemessage_view_default_view_mode', 'only_og_tags');
    $this
      ->drupalGet('sharemessage-test/1');
    $this
      ->assertShareButtons($edit, FALSE, TRUE);
    $this
      ->assertOGTags($edit['sharemessage_title[en][0][value]']);
    variable_set('sharemessage_view_default_view_mode', 'no_attributes');
    $this
      ->drupalGet('sharemessage-test/1');
    $this
      ->assertShareButtons($edit);
    $this
      ->assertOGTags($edit['sharemessage_title[en][0][value]']);
    variable_set('sharemessage_view_default_view_mode', 'attributes_only');
    $this
      ->drupalGet('sharemessage-test/1');
    $this
      ->assertShareButtons($edit, TRUE, FALSE);
    $this
      ->assertOGTags($edit['sharemessage_title[en][0][value]'], TRUE);
  }

  /**
   * Checks the markup of the share links wrapper.
   *
   * @param $sharemessage
   *   The edit array of a ShareMessage as passed to drupalPost().
   * @param bool $addthis_attributes
   *   Flag indicating whether or not to check for AddThis attributes.
   * @param bool $invert
   *   Set to true to invert the assertion, for example to make sure the share
   *   links don't get rendered.
   */
  protected function assertShareButtons($sharemessage, $addthis_attributes = FALSE, $invert = FALSE) {
    $raw_html_icon_style = '<div class="addthis_toolbox addthis_default_style addthis_16x16_style"';
    if ($addthis_attributes) {
      $raw_html_icon_style .= ' addthis:url="' . $sharemessage['sharemessage_url[und][0][value]'];
      $raw_html_icon_style .= '" addthis:title="' . $sharemessage['sharemessage_title[en][0][value]'];
      $raw_html_icon_style .= '" addthis:description="' . $sharemessage['sharemessage_long[en][0][value]'] . '"';
    }
    $raw_html_icon_style .= '>';
    if ($invert) {
      $this
        ->assertNoRaw($raw_html_icon_style);
    }
    else {
      $this
        ->assertRaw($raw_html_icon_style);
    }
  }

  /**
   * Checks for the markup of the OG meta tags.
   *
   * @param $title
   *   The title of the ShareMessage as should get printed to og:title.
   * @param bool $invert
   *   Set to true to invert the assertion, for example to make sure the OG tags
   *   are not rendered.
   */
  protected function assertOGTags($title, $invert = FALSE) {
    $meta_title = '<meta property="og:title" content="' . $title . '" />';
    if ($invert) {
      $this
        ->assertNoRaw($meta_title);
    }
    else {
      $this
        ->assertRaw($meta_title);
    }
  }

}