You are here

public function MediaBrowserPlusTest::testFolderMovemet in Media Browser Plus 7.3

Same name and namespace in other branches
  1. 7.2 tests/media_browser_plus.test \MediaBrowserPlusTest::testFolderMovemet()

Test the ability to move folders.

File

tests/media_browser_plus.test, line 104
Media Browser Plus basic tests.

Class

MediaBrowserPlusTest
Basic media browser plus tests.

Code

public function testFolderMovemet() {
  $this
    ->drupalLogin($this->adminUser);

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

  // Just to have a verbose output.
  $this
    ->drupalGet('admin/structure/taxonomy/media_folders');

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

  // Just to have a verbose output.
  $this
    ->drupalGet('admin/content/file/list');
  $source_path = media_browser_plus_construct_dir_path($folders['source']);

  // Now move the folder.
  $data['parent[]'] = array(
    $folders['destination']->tid,
  );
  $this
    ->drupalPost('taxonomy/term/' . $folders['source']->tid . '/edit', $data, t('Save'));

  // 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();
  $updated_source_path = media_browser_plus_construct_dir_path($folders['source']);

  // And now check if all files and folders were moved properly.
  foreach (media_get_local_stream_wrappers() as $scheme => $scheme_info) {
    $original_source_path_full = file_stream_wrapper_uri_normalize($scheme . '://' . $source_path);
    $updated_source_path_full = file_stream_wrapper_uri_normalize($scheme . '://' . $updated_source_path);

    // Check if the path of the source has changed.
    if ($this
      ->assertNotEqual($original_source_path_full, $updated_source_path_full, 'Source folder changed')) {

      // Check paths and files of source and its children.
      $new_parent_folder = 'destination';
      foreach (array(
        'source',
        'source_child',
        'source_child_child',
      ) as $type) {
        $folders[$type] = taxonomy_term_load($folders[$type]->tid);
        $source_path = file_stream_wrapper_uri_normalize($scheme . '://' . media_browser_plus_construct_dir_path($folders[$type]));
        $destination_path = file_stream_wrapper_uri_normalize($scheme . '://' . media_browser_plus_construct_dir_path($folders[$new_parent_folder]));

        // Check if the new path is a child folder of the destination.
        $this
          ->assertTrue(stristr($source_path, $destination_path) !== FALSE, 'Source folder is child folder of destination.');

        // Check if the old path was really deleted.
        $this
          ->assertFalse(is_dir($original_source_path_full), 'Old source folder deleted');

        // Now check the files.
        foreach ($files[$type][$scheme] 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, $destination_path) !== FALSE, 'Source file is child of new destination.');
        }
        $new_parent_folder = $type;
      }
    }
  }

  // Just to have a verbose output.
  $this
    ->drupalGet('admin/structure/taxonomy/media_folders');
}