<?php

/**
 * @file
 * Tests for media_wysiwyg_view_mode.module.
 */

/**
 * Defines base class for media_wysiwyg_view_mode test cases.
 */
class MediaWYSIWYGViewModeTestHelper extends MediaWYSIWYGTestHelper {
  function setUp() {
    parent::setUp('media_wysiwyg_view_mode');

    $web_user = $this->drupalCreateUser(array('administer media wysiwyg view mode', 'view files'));
    $this->drupalLogin($web_user);
  }
}

/**
 * Test configuring view modes available on the format form.
 */
class FormatFormViewModesTest extends MediaWYSIWYGViewModeTestHelper {
  public static function getInfo() {
    return array(
      'name' => 'Format Form WYSIWYG View Modes',
      'description' => 'Test configuring view modes available on the format form.',
      'group' => 'Media WYSIWYG View Mode',
    );
  }

  function setUp() {
    parent::setUp();
  }

  /**
   * Configure format form view mode restrictions and ensure that they are followed.
   */
  function testAllowedFormatFormViewModes() {
    // Load the Media WYSIWYG View Mode administration page.
    $this->drupalGet('admin/config/media/wysiwyg-view-mode');
    $this->assertResponse(200, t('The privileged user can access the Media WYSIWYG View Mode administration page.'));

    // Create an image file to test with.
    $files = $this->drupalGetTestFiles('image');
    $files[0]->status = FILE_STATUS_PERMANENT;
    $file = file_save($files[0]);
    $fid = $file->fid;

    // The default view mode should be selected by default.
    $this->drupalGet('media/' . $fid . '/format-form');
    $this->assertOptionSelected('edit-format', 'default');

    // Restrict the use of the default view mode.
    variable_set('media_wysiwyg_view_mode_image_wysiwyg_restricted_view_modes_status', TRUE);
    $restricted_view_modes = array(
      'default' => 'default',
    );
    variable_set('media_wysiwyg_view_mode_image_wysiwyg_restricted_view_modes', $restricted_view_modes);

    // The teaser view mode should now be selected by default.
    $this->drupalGet('media/' . $fid . '/format-form');
    $this->assertOptionSelected('edit-format', 'teaser');
  }
}
