Unverified Commit 8ab4de2f authored by Alex Pott's avatar Alex Pott Committed by GitHub
Browse files

Issue #3319701 by alexpott, daniel.bosen: Add Media File Delete integration...

Issue #3319701 by alexpott, daniel.bosen: Add Media File Delete integration and new permission to delete any file
parent 94044705
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -92,6 +92,7 @@
        "drupal/media_entity_slideshow": "^2.0-alpha1",
        "drupal/media_entity_twitter": "^2.5",
        "drupal/media_expire": "^2.4",
        "drupal/media_file_delete": "^1.2",
        "drupal/metatag": "^1.19",
        "drupal/metatag_async_widget": "^1.0-alpha2",
        "drupal/paragraphs": "^1.12",
+22 −0
Original line number Diff line number Diff line
@@ -5,7 +5,10 @@
 * Contains media related functions.
 */

use Drupal\Core\Access\AccessResult;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\file\FileInterface;

/**
 * Implements hook_preprocess_media().
@@ -177,3 +180,22 @@ function _thunder_media_media_field_widget_form_alter_helper(array &$widget, str
    }
  }
}

/**
 * Implements hook_ENTITY_TYPE_access().
 *
 * @todo Remove when https://www.drupal.org/project/drupal/issues/2949017 lands
 *   in Drupal core and the version it lands in is the minimum supported
 *   version.
 */
function thunder_media_file_access(FileInterface $file, string $op, AccountInterface $account): AccessResult {
  switch ($op) {
    case 'delete':
      $access = AccessResult::allowedIfHasPermission($account, 'delete any file');
      break;

    default:
      $access = AccessResult::neutral();
  }
  return $access;
}
+4 −0
Original line number Diff line number Diff line
# Match permission with https://www.drupal.org/project/drupal/issues/2949017
delete any file:
  title: 'Delete any file'
  restrict access: true
+18 −0
Original line number Diff line number Diff line
@@ -4,6 +4,7 @@

use Drupal\file\Entity\File;
use Drupal\image\Entity\ImageStyle;
use Drupal\user\Entity\Role;

/**
 * Tests the Image media modification.
@@ -95,6 +96,23 @@ public function testRemoveAdd(): void {
    $this->clickAjaxButtonCssSelector('[name="field_paragraphs_0_collapse"]');

    $this->assertEquals($fileName, $this->getSession()->evaluateScript('jQuery(\'[data-drupal-selector="edit-field-paragraphs-0-preview"] div.paragraph-preview__thumbnail img\').attr(\'src\').split(\'?\')[0].split(\'/\').splice(-1)'), 'Image file should be identical to previously selected.');

    // Go to the media view and try deleting the image media.
    $this->drupalGet('admin/content/media');
    $this->getSession()->getPage()->find('css', 'div.view-media')->clickLink('Thunder City');
    $media = $this->loadMediaByUuid('5d719c64-7f32-4062-9967-9874f5ca3eba');
    $this->assertSession()->addressMatches('#media/' . $media->id() . '/edit$#');
    /** @var \Drupal\file\FileInterface $file */
    $file = $media->get($media->getSource()->getConfiguration()['source_field'])->entity;
    $this->assertFileExists($file->getFileUri());
    $this->getSession()->getPage()->find('css', 'div.block-local-tasks-block')->clickLink('Delete');
    $this->assertSession()->fieldNotExists('also_delete_file');
    $this->assertSession()->pageTextContains('This action cannot be undone. The file attached to this media is owned by admin so will be retained.');
    Role::load(static::$defaultUserRole)->grantPermission('delete any file')->save();
    $this->getSession()->reload();
    $this->assertSession()->fieldExists('also_delete_file')->check();
    $this->getSession()->getPage()->pressButton('Delete');
    $this->assertFileDoesNotExist($file->getFileUri());
  }

}
+1 −0
Original line number Diff line number Diff line
@@ -66,6 +66,7 @@ install:
  - media_entity_slideshow:media_entity_slideshow
  - media_entity_twitter:media_entity_twitter
  - media_expire:media_expire
  - media_file_delete:media_file_delete
  - metatag:metatag
  - metatag:metatag_open_graph
  - metatag:metatag_facebook
Loading