Commit db3407bd authored by Joshua Sedler's avatar Joshua Sedler 🤸🏼
Browse files

Issue #3095839: Refactor code & write tests

parent bec82e67
Loading
Loading
Loading
Loading
+69 −60
Original line number Diff line number Diff line
<?php

/*
 TODO: See /core/modules/views/src/Plugin/views/field/LinkBase.php
 TODO: See https://drupal.stackexchange.com/questions/218824/how-to-alter-node-url-or-tourl-function-output
 TODO: Implement service
/**
 * @file
 * The rabbit_hole_href module file.
 *
 * @todo See /core/modules/views/src/Plugin/views/field/LinkBase.php
 * @todo See https://drupal.stackexchange.com/questions/218824/how-to-alter-node-url-or-tourl-function-output
 * @todo Implement service
 */

use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Url;

/**
 * @file
@@ -18,7 +20,12 @@ use Drupal\Core\Url;
 * Implements hook_entity_type_alter().
 */
function rabbit_hole_href_entity_type_alter(array &$entity_types) {
  // TODO - run this for all enabled entity types
  // @todo Determine the entity types which have rabbit_hole_href enabled as
  // rh_action, see Drupal\rabbit_hole\BehaviorInvoker, and go through them
  // instead of only taxonomy_term:
  // behaviourInvorker->getRabbitHoleValuesForEntityType($entity_type, $bundle);
  // NOTE: Currently the uri callback is always set, even if redirect is set to
  // 404 etc.!!
  $entity_types['taxonomy_term']->setUriCallback('rabbit_hole_href_redirect_uri');
  $links = $entity_types['taxonomy_term']->get('links');
  unset($links['canonical']);
@@ -29,8 +36,10 @@ function rabbit_hole_href_entity_type_alter(array &$entity_types) {
 * Implements hook_entity_bundle_info_alter().
 */
function rabbit_hole_href_entity_bundle_info_alter(&$bundles) {

  // kint($bundles['taxonomy_term']);
  // @todo Determine the entity types which have rabbit_hole_href enabled as
  // rh_action, see Drupal\rabbit_hole\BehaviorInvoker, and go through them
  // instead of only taxonomy_term. Also remove "jpdummy" and use redirect
  // instead:
  $bundles['taxonomy_term']['jpdummy']['uri_callback'] = 'rabbit_hole_href_redirect_uri';
  // // Set the uri callback for the redirect content type.
  // if (isset($bundles['taxonomy_term']['redirect'])) {
+73 −68
Original line number Diff line number Diff line
@@ -2,16 +2,15 @@

namespace Drupal\rabbit_hole_href;

use Drupal\rabbit_hole\Plugin\RabbitHoleBehaviorPluginManager;
use Drupal\Core\Entity\ContentEntityInterface;

use Drupal\rabbit_hole\Plugin\RabbitHoleBehaviorPluginManager;

/**
 * Interface CanonicalLinkModifier.
 * Modifies the canonical link.
 *
 * @package Drupal\rabbit_hole_href
 */
class CanonicalLinkModifier implements CanonicalLinkModifierInterface {
class CanonicalLinkModifier {

  /**
   * Drupal\rabbit_hole\Plugin\RabbitHoleBehaviorPluginManager definition.
@@ -27,11 +26,16 @@ class CanonicalLinkModifier implements CanonicalLinkModifierInterface {
    $this->rhBehaviorPluginManager = $plugin_manager_rabbit_hole_behavior_plugin;
  }

  /**
   * Get the original canonical url.
   */
  public function getCanonicalUrl(ContentEntityInterface $entity) {
    // TODO
    return $this->getNewCanonicalUrl($entity);
  }

  /**
   * Get the new url to redirect to.
   */
  protected function getNewCanonicalUrl(ContentEntityInterface $entity) {
    /*
    array(13) {
@@ -65,4 +69,5 @@ class CanonicalLinkModifier implements CanonicalLinkModifierInterface {
     */
    return $this->rhBehaviorPluginManager->createInstance('page_redirect_href')->getActionTarget($entity);
  }

}
+0 −13
Original line number Diff line number Diff line
<?php

namespace Drupal\rabbit_hole_href;

/**
 * Interface CanonicalLinkModifierInterface.
 *
 * @package Drupal\rabbit_hole_href
 */
interface CanonicalLinkModifierInterface {


}
+11 −23
Original line number Diff line number Diff line
@@ -6,21 +6,9 @@ use Drupal\Component\Utility\UrlHelper;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Url;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\Link;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Config\ImmutableConfig;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Utility\Token;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Render\BubbleableMetadata;
use Drupal\Core\Routing\TrustedRedirectResponse;
use Drupal\rabbit_hole\Plugin\RabbitHoleBehaviorPlugin\PageRedirect;
use Drupal\rabbit_hole\Exception\InvalidRedirectResponseException;
use Drupal\rabbit_hole\BehaviorSettingsManagerInterface;
use Drupal\rabbit_hole\Plugin\RabbitHoleEntityPluginManager;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Redirects to another page.
@@ -30,29 +18,28 @@ use Symfony\Component\DependencyInjection\ContainerInterface;
 *   label = @Translation("Page redirect with link override")
 * )
 */
class PageRedirectHref extends PageRedirect implements ContainerFactoryPluginInterface {
class PageRedirectHref extends PageRedirect {
  use StringTranslationTrait;

  /**
   * Returns the action target.
   *
   * Returns the action target like determined as redirect target in
   * Drupal\rabbit_hole\Plugin\RabbitHoleBehaviorPlugin\PageRedirect::performAction().
   *
   * @param EntityInterface $entity
   * @param \Drupal\Core\Entity\EntityInterface $entity
   *   The content entity to apply the direct redirect to.
   *
   * @return Symfony\Component\HttpFoundation\Response
   *   The response for the redirect.
   */
  public function getActionTarget(EntityInterface $entity) {
    // TODO: Write  a Drupal.org issue to make this part a
    // @todo Write a Drupal.org issue to make this part a
    // protected function in PageRedirect.
    // We had to duplicate this code because we can't get our target
    // from performAction().

    $target = $entity->get('rh_redirect')->value;

    $bundle_entity_type = $entity->getEntityType()->getBundleEntityType();
    $bundle_settings = $this->rhBehaviorSettingsManager
      ->loadBehaviorSettingsAsConfig(
        $bundle_entity_type ?: $entity->getEntityType()->id(),
        $bundle_entity_type ? $entity->bundle() : NULL);
    $bundle_settings = $this->getBundleSettings($entity);

    if (empty($target)) {
      $target = $bundle_settings->get('redirect');
@@ -90,4 +77,5 @@ class PageRedirectHref extends PageRedirect implements ContainerFactoryPluginInt

    return $target;
  }

}
+85 −0
Original line number Diff line number Diff line
<?php

namespace Drupal\Tests\rabbit_hole_href\Functional;

use Drupal\Tests\BrowserTestBase;

/**
 * This class provides methods specifically for testing rabbit_hole_href.
 *
 * @group rabbit_hole_href
 */
class RabbitHoleHrefFunctionalTest extends BrowserTestBase {

  /**
   * {@inheritdoc}
   */
  protected static $modules = [
    'node',
    'test_page_test',
    'taxonomy',
    'rabbit_hole',
    'rabbit_hole_href',
  ];

  /**
   * A user with authenticated permissions.
   *
   * @var \Drupal\Core\Session\AccountInterface
   */
  protected $user;

  /**
   * A user with admin permissions.
   *
   * @var \Drupal\Core\Session\AccountInterface
   */
  protected $adminUser;

  /**
   * {@inheritdoc}
   */
  protected $defaultTheme = 'stark';

  /**
   * {@inheritdoc}
   */
  protected function setUp(): void {
    parent::setUp();

    $this->config('system.site')->set('page.front', '/test-page')->save();
    $this->user = $this->drupalCreateUser([]);
    $this->adminUser = $this->drupalCreateUser([]);
    $this->adminUser->addRole($this->createAdminRole('admin', 'admin'));
    $this->adminUser->save();
    $this->drupalLogin($this->adminUser);
  }

  /**
   * Tests if the module installation, won't break the site.
   */
  public function testInstallation() {
    $session = $this->assertSession();
    $this->drupalGet('<front>');
    $session->statusCodeEquals(200);
  }

  /**
   * Tests if uninstalling the module, won't break the site.
   */
  public function testUninstallation() {
    // Go to uninstallation page an uninstall rabbit_hole_href:
    $session = $this->assertSession();
    $page = $this->getSession()->getPage();
    $this->drupalGet('/admin/modules/uninstall');
    $session->statusCodeEquals(200);
    $page->checkField('edit-uninstall-rabbit-hole-href');
    $page->pressButton('edit-submit');
    $session->statusCodeEquals(200);
    // Confirm deinstall:
    $page->pressButton('edit-submit');
    $session->statusCodeEquals(200);
    $session->pageTextContains('The selected modules have been uninstalled.');
  }

}
+1 −1

File changed.

Contains only whitespace changes.

Loading