You are here

class DrupalOrgTest in Freelinking 8.3

Same name and namespace in other branches
  1. 4.0.x tests/src/Unit/Plugin/freelinking/DrupalOrgTest.php \Drupal\Tests\freelinking\Unit\Plugin\freelinking\DrupalOrgTest

Tests the drupalorg plugin.

@group freelinking

Hierarchy

Expanded class hierarchy of DrupalOrgTest

File

tests/src/Unit/Plugin/freelinking/DrupalOrgTest.php, line 21

Namespace

Drupal\Tests\freelinking\Unit\Plugin\freelinking
View source
class DrupalOrgTest extends UnitTestCase {

  /**
   * The container.
   *
   * @var \Symfony\Component\DependencyInjection\ContainerInterface
   */
  protected $container;

  /**
   * String translation mock.
   *
   * @var \Drupal\Core\StringTranslation\TranslationInterface
   */
  protected $translationInterfaceMock;

  /**
   * {@inheritdoc}
   */
  protected function setUp() {

    // Mock string translation service.
    $tProphet = $this
      ->prophesize('\\Drupal\\Core\\StringTranslation\\TranslationInterface');
    $tProphet
      ->translateString(Argument::any())
      ->willReturn('Click to view on drupal.org.');
    $this->translationInterfaceMock = $tProphet
      ->reveal();

    // Mock module handler service.
    $moduleHandlerProphet = $this
      ->prophesize('\\Drupal\\Core\\Extension\\ModuleHandlerInterface');
    $moduleHandlerProphet
      ->moduleExists('search')
      ->willReturn(FALSE);
    $this->container = new ContainerBuilder();
    $this->container
      ->set('string_translation', $this->translationInterfaceMock);
    $this->container
      ->set('module_handler', $moduleHandlerProphet
      ->reveal());
    $this->container
      ->set('http_client', $this
      ->getGuzzleMock());
    \Drupal::setContainer($this->container);
  }

  /**
   * Assert that getTip() is functional.
   */
  public function testGetTip() {
    $plugin = $this
      ->getPlugin();
    $this
      ->assertEquals('Click to view on drupal.org.', $plugin
      ->getTip()
      ->render());
  }

  /**
   * Assert that the indicator is functional.
   *
   * @param string $indicator
   *   The indicator string.
   * @param array $settings
   *   The settings to test.
   * @param int $expected
   *   The expected value from preg_match.
   *
   * @dataProvider indicatorProvider
   */
  public function testGetIndicator($indicator, array $settings, $expected) {
    $plugin = $this
      ->getPlugin($settings);
    $this
      ->assertEquals($expected, preg_match($plugin
      ->getIndicator(), $indicator));
  }

  /**
   * Asserts that default configuration is expected.
   */
  public function testDefaultConfiguration() {
    $plugin = $this
      ->getPlugin();
    $expected = [
      'settings' => [
        'scrape' => TRUE,
        'node' => TRUE,
        'project' => TRUE,
      ],
    ];
    $this
      ->assertEquals($expected, $plugin
      ->defaultConfiguration());
  }

  /**
   * Asserts that buildLink returns appropriate render array.
   *
   * A data provider is not used for this test because Guzzle mocking is a bit
   * weird and unorthodox.
   */
  public function testBuildLink() {
    $plugin = $this
      ->getPlugin();
    $target = [
      'dest' => 'freelinking',
      'indicator' => 'drupalproject',
      'language' => NULL,
      'text' => '',
    ];
    $expected = [
      '#type' => 'link',
      '#title' => new TranslatableMarkup('Drupal.org: “@title”', [
        '@title' => 'Freelinking',
      ], [], $this->translationInterfaceMock),
      '#url' => Url::fromUri('https://drupal.org/project/freelinking', [
        'absolute' => TRUE,
        'language' => NULL,
      ]),
      '#attributes' => [
        'title' => new TranslatableMarkup('Click to view on drupal.org.', [], [], $this->translationInterfaceMock),
      ],
    ];

    // Assert that 200 Response with title.
    $this
      ->assertEquals($expected, $plugin
      ->buildLink($target));
  }

  /**
   * Get plugin instance.
   *
   * @param array $default_settings
   *   The settings to use.
   *
   * @return \Drupal\freelinking\Plugin\freelinking\DrupalOrg
   *   A plugin instance.
   */
  protected function getPlugin(array $default_settings = []) {
    $settings = $default_settings + [
      'scrape' => TRUE,
      'node' => TRUE,
      'project' => TRUE,
    ];
    $configuration = [
      'settings' => $settings,
    ];
    $plugin_definition = [
      'id' => 'drupalorg',
      'title' => 'Drupal.org External link',
      'hidden' => FALSE,
      'weight' => 0,
    ] + $configuration;
    return DrupalOrg::create($this->container, $configuration, 'external', $plugin_definition);
  }

  /**
   * Create Guzzle client instance with mock handlers.
   *
   * @return \GuzzleHttp\Client
   *   The Guzzle HTTP Client.
   */
  protected function getGuzzleMock() {
    $mock = new MockHandler([
      new Response(200, [
        'Content-Type' => 'text/html',
      ], '<body><h1 class="page-subtitle">Freelinking</h1><div>Test Page Content.</div></body>'),
    ]);
    $handler = HandlerStack::create($mock);
    return new Client([
      'handler' => $handler,
    ]);
  }

  /**
   * Provide test parameters for ::testGetIndicator.
   *
   * @return array
   *   An array of test parameters.
   */
  public function indicatorProvider() {
    return [
      [
        'nomatch',
        [
          'node' => TRUE,
        ],
        0,
      ],
      [
        'nomatch',
        [
          'project' => TRUE,
        ],
        0,
      ],
      [
        'dorg',
        [
          'node' => TRUE,
        ],
        1,
      ],
      [
        'dorg',
        [
          'node' => FALSE,
        ],
        0,
      ],
      [
        'drupalorg',
        [
          'node' => TRUE,
        ],
        1,
      ],
      [
        'drupalo',
        [
          'node' => TRUE,
        ],
        1,
      ],
      [
        'drupalproject',
        [
          'project' => TRUE,
        ],
        1,
      ],
      [
        'drupalp',
        [
          'project' => TRUE,
        ],
        1,
      ],
      [
        'dproject',
        [
          'project' => TRUE,
        ],
        1,
      ],
      [
        'drupalproject',
        [
          'project' => FALSE,
        ],
        0,
      ],
    ];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DrupalOrgTest::$container protected property The container.
DrupalOrgTest::$translationInterfaceMock protected property String translation mock.
DrupalOrgTest::getGuzzleMock protected function Create Guzzle client instance with mock handlers.
DrupalOrgTest::getPlugin protected function Get plugin instance.
DrupalOrgTest::indicatorProvider public function Provide test parameters for ::testGetIndicator.
DrupalOrgTest::setUp protected function Overrides UnitTestCase::setUp
DrupalOrgTest::testBuildLink public function Asserts that buildLink returns appropriate render array.
DrupalOrgTest::testDefaultConfiguration public function Asserts that default configuration is expected.
DrupalOrgTest::testGetIndicator public function Assert that the indicator is functional.
DrupalOrgTest::testGetTip public function Assert that getTip() is functional.
PhpunitCompatibilityTrait::getMock Deprecated public function Returns a mock object for the specified class using the available method.
PhpunitCompatibilityTrait::setExpectedException Deprecated public function Compatibility layer for PHPUnit 6 to support PHPUnit 4 code.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName Deprecated protected function Mocks a block with a block plugin. 1
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.