Skip to content
ForumUninstallValidatorTest.php 7.53 KiB
Newer Older
<?php

/**
 * @file
 * Contains \Drupal\Tests\forum\Unit\ForumUninstallValidatorTest.
 */

namespace Drupal\Tests\forum\Unit;

use Drupal\Component\Utility\SafeMarkup;
use Drupal\Tests\UnitTestCase;

/**
 * @coversDefaultClass \Drupal\forum\ForumUninstallValidator
 * @group forum
 */
class ForumUninstallValidatorTest extends UnitTestCase {

  /**
   * @var \Drupal\forum\ForumUninstallValidator|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $forumUninstallValidator;

  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    parent::setUp();
    $this->forumUninstallValidator = $this->getMockBuilder('Drupal\forum\ForumUninstallValidator')
      ->disableOriginalConstructor()
      ->setMethods(['hasForumNodes', 'hasTermsForVocabulary', 'getForumVocabulary'])
      ->getMock();
    $this->forumUninstallValidator->setStringTranslation($this->getStringTranslationStub());
  }

  /**
   * @covers ::validate
   */
  public function testValidateNotForum() {
    $this->forumUninstallValidator->expects($this->never())
      ->method('hasForumNodes');
    $this->forumUninstallValidator->expects($this->never())
      ->method('hasTermsForVocabulary');
    $this->forumUninstallValidator->expects($this->never())
      ->method('getForumVocabulary');

    $module = 'not_forum';
    $expected = [];
    $reasons = $this->forumUninstallValidator->validate($module);
    $this->assertSame($expected, $reasons);
  }

  /**
   * @covers ::validate
   */
  public function testValidate() {
    $this->forumUninstallValidator->expects($this->once())
      ->method('hasForumNodes')
      ->willReturn(FALSE);

    $vocabulary = $this->getMock('Drupal\taxonomy\VocabularyInterface');
    $this->forumUninstallValidator->expects($this->once())
      ->method('getForumVocabulary')
      ->willReturn($vocabulary);

    $this->forumUninstallValidator->expects($this->once())
      ->method('hasTermsForVocabulary')
      ->willReturn(FALSE);

    $module = 'forum';
    $expected = [];
    $reasons = $this->forumUninstallValidator->validate($module);
    $this->assertSame($expected, $reasons);
  }

  /**
   * @covers ::validate
   */
  public function testValidateHasForumNodes() {
    $this->forumUninstallValidator->expects($this->once())
      ->method('hasForumNodes')
      ->willReturn(TRUE);

    $vocabulary = $this->getMock('Drupal\taxonomy\VocabularyInterface');
    $this->forumUninstallValidator->expects($this->once())
      ->method('getForumVocabulary')
      ->willReturn($vocabulary);

    $this->forumUninstallValidator->expects($this->once())
      ->method('hasTermsForVocabulary')
      ->willReturn(FALSE);

    $module = 'forum';
    $expected = [
      'To uninstall Forum, first delete all <em>Forum</em> content',
    ];
    $reasons = $this->forumUninstallValidator->validate($module);
    $this->assertSame($expected, $reasons);
  }

  /**
   * @covers ::validate
   */
  public function testValidateHasTermsForVocabularyWithNodesAccess() {
    $this->forumUninstallValidator->expects($this->once())
      ->method('hasForumNodes')
      ->willReturn(TRUE);

    $vocabulary = $this->getMock('Drupal\taxonomy\VocabularyInterface');
    $vocabulary->expects($this->once())
      ->method('label')
      ->willReturn('Vocabulary label');
    $vocabulary->expects($this->once())
      ->method('url')
      ->willReturn('/path/to/vocabulary/overview');
    $vocabulary->expects($this->once())
      ->method('access')
      ->willReturn(TRUE);
    $this->forumUninstallValidator->expects($this->once())
      ->method('getForumVocabulary')
      ->willReturn($vocabulary);

    $this->forumUninstallValidator->expects($this->once())
      ->method('hasTermsForVocabulary')
      ->willReturn(TRUE);

    $module = 'forum';
    $expected = [
      'To uninstall Forum, first delete all <em>Forum</em> content',
      SafeMarkup::format('To uninstall Forum, first delete all <a href="@url">%vocabulary</a> terms', [
        '@url' => '/path/to/vocabulary/overview',
        '%vocabulary' => 'Vocabulary label',
      ]),
    ];
    $reasons = $this->forumUninstallValidator->validate($module);
    $this->assertSame($expected, $reasons);
  }

  /**
   * @covers ::validate
   */
  public function testValidateHasTermsForVocabularyWithNodesNoAccess() {
    $this->forumUninstallValidator->expects($this->once())
      ->method('hasForumNodes')
      ->willReturn(TRUE);

    $vocabulary = $this->getMock('Drupal\taxonomy\VocabularyInterface');
    $vocabulary->expects($this->once())
      ->method('label')
      ->willReturn('Vocabulary label');
    $vocabulary->expects($this->never())
      ->method('url');
    $vocabulary->expects($this->once())
      ->method('access')
      ->willReturn(FALSE);
    $this->forumUninstallValidator->expects($this->once())
      ->method('getForumVocabulary')
      ->willReturn($vocabulary);

    $this->forumUninstallValidator->expects($this->once())
      ->method('hasTermsForVocabulary')
      ->willReturn(TRUE);

    $module = 'forum';
    $expected = [
      'To uninstall Forum, first delete all <em>Forum</em> content',
      SafeMarkup::format('To uninstall Forum, first delete all %vocabulary terms', [
        '%vocabulary' => 'Vocabulary label',
      ]),
    ];
    $reasons = $this->forumUninstallValidator->validate($module);
    $this->assertSame($expected, $reasons);
  }

  /**
   * @covers ::validate
   */
  public function testValidateHasTermsForVocabularyAccess() {
    $this->forumUninstallValidator->expects($this->once())
      ->method('hasForumNodes')
      ->willReturn(FALSE);

    $vocabulary = $this->getMock('Drupal\taxonomy\VocabularyInterface');
    $vocabulary->expects($this->once())
      ->method('url')
      ->willReturn('/path/to/vocabulary/overview');
    $vocabulary->expects($this->once())
      ->method('label')
      ->willReturn('Vocabulary label');
    $vocabulary->expects($this->once())
      ->method('access')
      ->willReturn(TRUE);
    $this->forumUninstallValidator->expects($this->once())
      ->method('getForumVocabulary')
      ->willReturn($vocabulary);

    $this->forumUninstallValidator->expects($this->once())
      ->method('hasTermsForVocabulary')
      ->willReturn(TRUE);

    $module = 'forum';
    $expected = [
      SafeMarkup::format('To uninstall Forum, first delete all <a href="@url">%vocabulary</a> terms', [
        '@url' => '/path/to/vocabulary/overview',
        '%vocabulary' => 'Vocabulary label',
      ]),
    ];
    $reasons = $this->forumUninstallValidator->validate($module);
    $this->assertSame($expected, $reasons);
  }

  /**
   * @covers ::validate
   */
  public function testValidateHasTermsForVocabularyNoAccess() {
    $this->forumUninstallValidator->expects($this->once())
      ->method('hasForumNodes')
      ->willReturn(FALSE);

    $vocabulary = $this->getMock('Drupal\taxonomy\VocabularyInterface');
    $vocabulary->expects($this->once())
      ->method('label')
      ->willReturn('Vocabulary label');
    $vocabulary->expects($this->never())
      ->method('url');
    $vocabulary->expects($this->once())
      ->method('access')
      ->willReturn(FALSE);
    $this->forumUninstallValidator->expects($this->once())
      ->method('getForumVocabulary')
      ->willReturn($vocabulary);

    $this->forumUninstallValidator->expects($this->once())
      ->method('hasTermsForVocabulary')
      ->willReturn(TRUE);

    $module = 'forum';
    $expected = [
      SafeMarkup::format('To uninstall Forum, first delete all %vocabulary terms', [
        '%vocabulary' => 'Vocabulary label',
      ]),
    ];
    $reasons = $this->forumUninstallValidator->validate($module);
    $this->assertSame($expected, $reasons);
  }

}