You are here

public function MediaBrowserPlusTest::testMovingRootFolder in Media Browser Plus 7.2

Test the ability to move the root folder for media files.

File

tests/media_browser_plus.test, line 377
Media Browser Plus tests.

Class

MediaBrowserPlusTest
Defines media entity creation and management test cases.

Code

public function testMovingRootFolder() {
  $this
    ->drupalLogin($this->admin_user);

  // Create test folder structure.
  $folders = array(
    'source' => $this
      ->randomName(),
    'source_child' => $this
      ->randomName(),
    'source_child_child' => $this
      ->randomName(),
  );
  $hierarchy = array(
    'root_folder' => array(
      $folders['source'] => array(
        $folders['source_child'] => array(
          $folders['source_child_child'] => array(),
        ),
      ),
    ),
  );
  $this
    ->folderCreationHelper($hierarchy, FALSE);

  // Create test files.
  $files = array();
  foreach ($folders as $type => $folder_name) {
    $terms = taxonomy_get_term_by_name($folder_name);
    $folder = $folders[$type] = reset($terms);
    $files[] = $this
      ->createTestFile('text/plain', $folder);
    $files[] = $this
      ->createTestFile('image/jpg', $folder);
  }

  // Just to have a verbose output.
  $this
    ->drupalGet('admin/content/file/list');

  // Create mbp independent folders and files.
  $scheme = variable_get('file_default_scheme', 'public') . '://';
  $independent_folder = drupal_realpath($scheme) . '/non_mbp';
  file_prepare_directory($independent_folder, FILE_CREATE_DIRECTORY);
  $independent_files = array(
    $independent_folder . '/non_mpp.txt',
    drupal_realpath($scheme) . '/non_mpp.txt',
  );
  foreach ($independent_files as $independent_file) {
    file_put_contents($independent_file, str_repeat('01', 512));
  }

  // Reconfigure the root folder.
  $data = array(
    'root_folder' => 'mbp',
    'max_filesize' => '100MB',
  );
  $this
    ->drupalPost('admin/config/media/media_browser_plus_settings', $data, t('Save Changes'));

  // Reset the different caches.
  drupal_static_reset('taxonomy_get_parents');
  drupal_static_reset('taxonomy_get_parents_all');
  entity_get_controller('taxonomy_term')
    ->resetCache();
  entity_get_controller('file')
    ->resetCache();
  clearstatcache();

  // Check if managed files were moved.
  foreach ($files as $file) {

    // Refresh cached object.
    $file = file_load($file->fid);

    // Check if the new path is a child folder of the destination.
    $this
      ->assertTrue(stristr($file->uri, $scheme . 'mbp/') !== FALSE, 'Managed file is moved to new destination.');
  }

  // Check if independent folder wasn't moved.
  $this
    ->assertTrue(is_dir($independent_folder), 'Independent folder is untouched.');

  // Check if independent files weren't moved.
  foreach ($independent_files as $file) {
    $this
      ->assertTrue(file_exists($file), 'Independent file is untouched.');
  }

  // And move all the stuff back.
  // Reconfigure the root folder.
  $data = array(
    'root_folder' => '',
  );
  $this
    ->drupalPost('admin/config/media/media_browser_plus_settings', $data, t('Save Changes'));

  // Reset the different caches.
  drupal_static_reset('taxonomy_get_parents');
  drupal_static_reset('taxonomy_get_parents_all');
  entity_get_controller('taxonomy_term')
    ->resetCache();
  entity_get_controller('file')
    ->resetCache();
  clearstatcache();

  // Check if managed files were moved.
  foreach ($files as $file) {

    // Refresh cached object.
    $file = file_load($file->fid);

    // Check if the new path is a child folder of the destination.
    $this
      ->assertFalse(stristr($file->uri, $scheme . 'mpb/'), 'Managed file is moved to new destination.');
  }

  // Check if independent folder wasn't moved.
  $this
    ->assertTrue(is_dir($independent_folder), 'Independent folder is untouched.');

  // Check if independent files weren't moved.
  foreach ($independent_files as $file) {
    $this
      ->assertTrue(file_exists($file), 'Independent file is untouched.');
  }
}