<?php

/**
 * @file
 * Tests for media types.
 */

/**
 * Test media type creation and management.
 */
class MediaTypeTest extends DrupalWebTestCase {

  public static function getInfo() {
    return array(
      'name' => 'Media types',
      'description' => 'Tests media types',
      'group' => 'Media',
      'dependencies' => array('ctools'),
    );
  }

  function setUp() {
    parent::setUp('media');
    // Nice, TDD FTW.  #totalsarcasm
    variable_set('simpletest_verbose', TRUE);
  }

  private function createType($overrides) {
    $type = new stdClass();
    $type->name = 'test';
    $type->label = "Test";
    $type->base = TRUE;
//    $type->view_mode_defaults = array(
//      'media_preview' => 'styles_file_square_thumbnail',
//      'media_original' => 'file_default',
//    );

    $type->type_callback_args =
    array(
      'match_type' => 'all',
      'mimetypes' => array('/^test/'),
      'extensions' => array('jpg', 'jpeg', 'gif', 'png', 'tiff'),
      'streams' => array('public', 'private'),
    );

    foreach ($overrides as $k => $v) {
      $type->$k = $v;
    }

    media_type_save($type);
    return $type;
  }

  /**
   * Test creating a new type. Basic CRUD.
   */
  function testCreate() {
    $type_machine_name = 'foo';
    $type = $this->createType(array('name' => $type_machine_name, 'label' => 'foobar'));
    $loaded_type = media_type_load($type_machine_name);
    $this->assertEqual($loaded_type->label, 'foobar', "Was able to create a type and retreive it");
  }

  /**
   * Ensures that the weight is respected when types are created.
   * @return unknown_type
   */
  function testOrder() {
    $type = $this->createType(array('name' => 'last', 'label' => 'Last', 'weight' => 100));
    $type = $this->createType(array('name' => 'first', 'label' => 'First'));
    $types = media_type_get_types();
    $keys = array_keys($types);
    $this->assertTrue(isset($types['last']) && isset($types['first']), "Both types saved");
    $this->assertTrue(array_search('last', $keys) > array_search('first', $keys), 'Type which was supposed to be first came first');
  }

  /**
   * Test view mode assignment.  Currently fails, don't know why.
   * @return unknown_type
   */
  function testViewModesAssigned() {
  }

}
