Commit da6bc22c authored by Youri van Koppen's avatar Youri van Koppen Committed by Youri van Koppen
Browse files

Issue #2904668 by MegaChriz, bruno.bicudo, vsujeetkumar, mrinalini9,...

Issue #2904668 by MegaChriz, bruno.bicudo, vsujeetkumar, mrinalini9, WagnerMelo, chegor, sportch: Explain why a feed type cannot be deleted in the UI.
parent 782b1ee7
Loading
Loading
Loading
Loading
+1 −10
Original line number Diff line number Diff line
@@ -25,17 +25,8 @@ class FeedTypeAccessControlHandler extends EntityAccessControlHandler {
        $has_perm = $account->hasPermission('administer feeds') || $account->hasPermission("view {$entity->id()} feeds");
        return AccessResult::allowedIf($has_perm);

        break;

      case 'delete':
        if ($entity->isNew()) {
          return AccessResult::forbidden()->addCacheableDependency($entity);
        }
        // The delete operation is not cacheable since the locked status can
        // change in the background.
        return AccessResult::allowedIf($account->hasPermission('administer feeds') && !$entity->isLocked())->addCacheableDependency(FALSE);

        break;
        return parent::checkAccess($entity, $operation, $account)->addCacheableDependency($entity);

      default:
        return parent::checkAccess($entity, $operation, $account);
+22 −32
Original line number Diff line number Diff line
@@ -2,47 +2,37 @@

namespace Drupal\feeds\Form;

use Drupal\Core\Entity\EntityConfirmFormBase;
use Drupal\Core\Entity\EntityDeleteForm;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;

/**
 * Provides a form for deleting a feed type.
 * Provides a form for feed type deletion.
 */
class FeedTypeDeleteForm extends EntityConfirmFormBase {
class FeedTypeDeleteForm extends EntityDeleteForm {

  /**
   * {@inheritdoc}
   */
  public function getQuestion() {
    return $this->t('Are you sure you want to delete the feed type %type?', ['%type' => $this->entity->label()]);
  public function buildForm(array $form, FormStateInterface $form_state) {
    $num_feeds = $this->entityTypeManager->getStorage('feeds_feed')
      ->getQuery()
      ->accessCheck(FALSE)
      ->condition('type', $this->entity->id())
      ->count()
      ->execute();
    if ($num_feeds) {
      $caption = '<p>' . $this->formatPlural($num_feeds, '%type is used by 1 feed on your site. You can not remove this feed type until you have removed all of the %type feeds. <a href=":link">Return to feed types list.</a>', '%type is used by @count feeds on your site. You may not remove %type until you have removed all of the %type feeds. <a href=":link">Return to feed types list.</a>', [
        '%type' => $this->entity->label(),
        ':link' => '/admin/structure/feeds',
      ]) . '</p>';
      $form['#title'] = $this->getQuestion();
      $form['description'] = ['#markup' => $caption];

      return $form;
    }

  /**
   * {@inheritdoc}
   */
  public function getCancelUrl() {
    return new Url('entity.feeds_feed_type.collection');
  }

  /**
   * {@inheritdoc}
   */
  public function getConfirmText() {
    return $this->t('Delete');
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $this->entity->delete();

    $args = ['%type' => $this->entity->label()];
    $this->logger('feeds')->notice('Deleted feed type: %type.', $args);
    $this->messenger()->addMessage($this->t('%type has been deleted.', $args));

    $form_state->setRedirectUrl($this->getCancelUrl());
    return parent::buildForm($form, $form_state);
  }

}
+33 −0
Original line number Diff line number Diff line
@@ -19,6 +19,7 @@ class FeedTypeTest extends FeedsBrowserTestBase {
    'feeds',
    'node',
    'user',
    'block',
  ];

  /**
@@ -151,4 +152,36 @@ class FeedTypeTest extends FeedsBrowserTestBase {
    $this->assertTrue($processor['authorize']);
  }

  /**
   * Tests deleting a feed type that has content.
   */
  public function testFeedTypeWithContentDeletion() {
    $this->drupalPlaceBlock('page_title_block');

    // Create a feed type programmatically.
    $feed_type = $this->createFeedType();
    $feed_type_label = $feed_type->label();
    // Add a feed.
    $feed = $this->createFeed($feed_type->id(), [
      'source' => $this->resourcesPath() . '/rss/googlenewstz.rss2',
    ]);

    // Attempt to delete the feed type, which should not be allowed.
    $this->drupalGet('admin/structure/feeds/manage/' . $feed_type->id() . '/delete');
    $this->assertSession()->pageTextContains("$feed_type_label is used by 1 feed on your site. You can not remove this feed type until you have removed all of the $feed_type_label feeds.");
    $this->assertSession()->pageTextNotContains('This action cannot be undone.');

    // Delete the feed.
    $feed->delete();
    // Attempt to delete the feed type, which should now be allowed.
    $this->drupalGet('admin/structure/feeds/manage/' . $feed_type->id() . '/delete');
    $this->assertSession()->pageTextContains("Are you sure you want to delete the feed type $feed_type_label?");
    $this->assertSession()->pageTextContains('This action cannot be undone.');

    // Confirm deletion.
    $this->submitForm([], 'Delete');
    $this->assertNull($this->reloadEntity($feed_type), 'The feed type is deleted.');
    $this->assertSession()->pageTextContains("The feed type $feed_type_label has been deleted.");
  }

}
+1 −10
Original line number Diff line number Diff line
@@ -53,25 +53,16 @@ class FeedTypeAccessControlHandlerTest extends FeedsUnitTestCase {
    ]);
    $this->assertTrue($result->isAllowed());

    $result = $method->invokeArgs($this->controller, [
      $this->entity->reveal(),
      'delete',
      $this->account->reveal(),
    ]);
    $this->assertTrue($result->isAllowed());

    $this->entity->getCacheContexts()->willReturn([]);
    $this->entity->getCacheTags()->willReturn([]);
    $this->entity->getCacheMaxAge()->willReturn(0);

    $this->entity->isLocked()->willReturn(TRUE);
    $this->entity->isNew()->willReturn(FALSE);
    $result = $method->invokeArgs($this->controller, [
      $this->entity->reveal(),
      'delete',
      $this->account->reveal(),
    ]);
    $this->assertFalse($result->isAllowed());
    $this->assertTrue($result->isAllowed());

    $this->account->hasPermission('administer feeds')->willReturn(FALSE);
    $result = $method->invokeArgs($this->controller, [