<?php

/**
 * @file
 * Tests for media entity controllers.
 */

/**
 * Defines media entity creation and management test cases.
 */
class MediaEntityTest extends MediaTestHelper {

  /**
   * Provides test information.
   */
  public static function getInfo() {
    return array(
      'name' => 'Media entity',
      'description' => 'Tests media entity handling',
      'group' => 'Media',
    );
  }

  /**
   * Test the ability to create and query media items.
   */
  public function testQueryMedia() {
    $text_files = $this->drupalGetTestFiles('text');
    $images = $this->drupalGetTestFiles('image');

    // Moves serveral images to public://
    foreach (array_merge($text_files, $images) as $file) {
      // file_uri_to_object triggers a fatal error to d.o testbot.
      // lets remove for now..
      // $file = file_uri_to_object($file->uri);
      file_save($file);
    }

    $query = new EntityFieldQuery();
    $query->entityCondition('entity_type', 'file');
    $query->propertyCondition('uri', 'public://%', 'LIKE');
    $result = $query->execute();
    $this->assertEqual(count($result['file']), count($images) + count($text_files), 'Returned results as expected for like % condition.');

    $query = new EntityFieldQuery();
    $query->entityCondition('entity_type', 'file');
    $query->propertyCondition('uri', '%.jpg', 'LIKE');
    $result = $query->execute();
    $this->assertEqual(count($result['file']), 2, 'Returned 2 results as expected for jpgs.');

    $query = new EntityFieldQuery();
    $query->entityCondition('entity_type', 'file');
    $query->propertyCondition('type', 'image');
    $result = $query->execute();
    $this->assertEqual(count($result['file']), count($images), 'Returned expected results for type query.');

    $query = new EntityFieldQuery();
    $query->entityCondition('entity_type', 'file');
    $query->propertyCondition('uri', 'http://%', 'LIKE');
    $result = $query->execute();
    $this->assertEqual(count($result), 0, 'Returned 0 results as expected for http scheme uris.');
  }
}
