You are here

responsive_preview.test in Responsive Theme Preview 7

File

responsive_preview.test
View source
<?php

/**
 * @file
 * Tests for responsive_preview.module.
 */
class ResponsivePreviewDeviceCRUD extends DrupalWebTestCase {

  /**
   * A user with permission to modify site configuration.
   *
   * @var object
   */
  protected $admin_user;
  public static function getInfo() {
    return array(
      'name' => 'Responsive Preview',
      'description' => 'CRUD tests for Responsive Preview devices.',
      'group' => 'Responsive Preview',
    );
  }
  function setUp() {
    parent::setUp(array(
      'responsive_preview',
    ));

    // Create users and test node.
    $this->admin_user = $this
      ->drupalCreateUser(array(
      'administer site configuration',
      'administer blocks',
    ));
  }

  /**
   *
   */
  function testDeviceConfiguration() {

    // Create and login administrative user.
    $admin_user = $this->admin_user;
    $this
      ->drupalLogin($admin_user);

    // Set the controls block to a region to confirm block is available.
    $edit = array();
    $edit['blocks[responsive_preview_controls][region]'] = 'sidebar_first';
    $this
      ->drupalPost('admin/structure/block', $edit, t('Save blocks'));
    $this
      ->assertText(t('The block settings have been updated.'), 'Block successfully move to sidebar_first region.');

    // Assert
    $this
      ->drupalGet('admin/config/content/responsive-preview');

    // Some default devices exist.
    $this
      ->assertLinkByHref('admin/config/content/responsive-preview/large/delete');
    $this
      ->assertLinkByHref('admin/config/content/responsive-preview/iphone6/delete');

    // Some devices are shown by default.
    $this
      ->drupalGet('');
    $this
      ->checkDevices(array(
      'iphone6',
      'iphone6p',
      'nexus5',
      'nexus6',
      'nexus9',
    ));

    // Delete one of the predefined devices.
    $this
      ->drupalPost('admin/config/content/responsive-preview/iphone6/delete', array(), t('Delete'));
    $this
      ->assertRaw(t('Device %name has been deleted.', array(
      '%name' => 'iPhone 6',
    )));

    // Make generic tablet appear in the list.
    $this
      ->drupalPost('admin/config/content/responsive-preview', array(
      'devices[medium][status]' => 1,
    ), t('Save'));
    $this
      ->assertRaw(t('The device settings have been updated.'));

    // Add a new device as well.
    $edit = array(
      'device[label]' => 'Tarsier',
      'device[name]' => 'tarsier',
      'device[dimensions][width]' => '200',
      'device[dimensions][height]' => '350',
      'device[dimensions][dppx]' => '3',
    );
    $this
      ->drupalPost('admin/config/content/responsive-preview/add', $edit, t('Add device'));
    $this
      ->assertRaw(t('Device %name has been added.', array(
      '%name' => 'Tarsier',
    )));

    // Check updated device list. New devices are shown in the list by default.
    $this
      ->drupalGet('');
    $this
      ->checkDevices(array(
      'tarsier',
      'iphone6',
      'iphone6p',
      'nexus5',
      'nexus6',
      'nexus9',
    ));

    // Update an existing device.
    $edit = array(
      'device[dimensions][width]' => '1600',
      'device[dimensions][height]' => '2850',
      'device[dimensions][dppx]' => '2',
    );

    // Delete one of the predefined devices.
    $this
      ->drupalPost('admin/config/content/responsive-preview/iphone6/edit', $edit, t('Save'));
    $this
      ->assertRaw(t('Device %name has been updated.', array(
      '%name' => 'iPhone 6',
    )));
  }

  /**
   * Tests exposed devices in the responsive preview list.
   */
  private function checkDevices($devices = array()) {
    foreach ($devices as $name) {
      $device_button = $this
        ->xpath('//button[@data-responsive-preview-name=:name]', array(
        ':name' => $name,
      ));
      $this
        ->assertTrue(!empty($device_button), format_string('%name device shown by default', array(
        '%name' => $name,
      )));
    }
  }

}

Classes