SubstitutionPluginTest.php 2.87 KB
Newer Older
1 2 3 4 5 6
<?php

namespace Drupal\Tests\linkit\Kernel;

use Drupal\entity_test\Entity\EntityTest;
use Drupal\file\Entity\File;
7 8
use Drupal\linkit\Plugin\Linkit\Substitution\Canonical as CanonicalSubstitutionPlugin;
use Drupal\linkit\Plugin\Linkit\Substitution\File as FileSubstitutionPlugin;
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24

/**
 * Tests the substitution plugins.
 *
 * @group linkit
 */
class SubstitutionPluginTest extends LinkitKernelTestBase {

  /**
   * The substitution manager.
   *
   * @var \Drupal\linkit\SubstitutionManagerInterface
   */
  protected $substitutionManager;

  /**
25
   * The entity type manager.
26
   *
27
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
28
   */
29
  protected $entityTypeManager;
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46

  /**
   * Additional modules to enable.
   *
   * @var array
   */
  public static $modules = [
    'file',
    'entity_test',
  ];

  /**
   * {@inheritdoc}
   */
  public function setUp() {
    parent::setUp();
    $this->substitutionManager = $this->container->get('plugin.manager.linkit.substitution');
47
    $this->entityTypeManager = $this->container->get('entity_type.manager');
48 49 50 51 52 53 54 55 56

    $this->installEntitySchema('file');
    $this->installEntitySchema('entity_test');
  }

  /**
   * Test the file substitution.
   */
  public function testFileSubstitutions() {
57
    $fileSubstitution = $this->substitutionManager->createInstance('file');
58 59 60 61 62 63 64 65
    $file = File::create([
      'uid' => 1,
      'filename' => 'druplicon.txt',
      'uri' => 'public://druplicon.txt',
      'filemime' => 'text/plain',
      'status' => FILE_STATUS_PERMANENT,
    ]);
    $file->save();
66 67 68 69 70 71 72
    $this->assertEquals($GLOBALS['base_url'] . '/' . $this->siteDirectory . '/files/druplicon.txt', $fileSubstitution->getUrl($file)->getGeneratedUrl());

    $entity_type = $this->entityTypeManager->getDefinition('file');
    $this->assertTrue(FileSubstitutionPlugin::isApplicable($entity_type), 'The entity type File is applicable the file substitution.');

    $entity_type = $this->entityTypeManager->getDefinition('entity_test');
    $this->assertFalse(FileSubstitutionPlugin::isApplicable($entity_type), 'The entity type EntityTest is not applicable the file substitution.');
73 74 75
  }

  /**
76
   * Test the canonical substitution.
77
   */
78 79
  public function testCanonicalSubstitution() {
    $canonicalSubstitution = $this->substitutionManager->createInstance('canonical');
80 81
    $entity = EntityTest::create([]);
    $entity->save();
82 83 84 85 86 87 88
    $this->assertEquals('/entity_test/1', $canonicalSubstitution->getUrl($entity)->getGeneratedUrl());

    $entity_type = $this->entityTypeManager->getDefinition('entity_test');
    $this->assertTrue(CanonicalSubstitutionPlugin::isApplicable($entity_type), 'The entity type EntityTest is applicable the canonical substitution.');

    $entity_type = $this->entityTypeManager->getDefinition('file');
    $this->assertFalse(CanonicalSubstitutionPlugin::isApplicable($entity_type), 'The entity type File is not applicable the canonical substitution.');
89 90 91
  }

}