You are here

class ForumUninstallValidatorTest in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/forum/tests/src/Unit/ForumUninstallValidatorTest.php \Drupal\Tests\forum\Unit\ForumUninstallValidatorTest

@coversDefaultClass \Drupal\forum\ForumUninstallValidator @group forum

Hierarchy

Expanded class hierarchy of ForumUninstallValidatorTest

File

core/modules/forum/tests/src/Unit/ForumUninstallValidatorTest.php, line 17
Contains \Drupal\Tests\forum\Unit\ForumUninstallValidatorTest.

Namespace

Drupal\Tests\forum\Unit
View source
class ForumUninstallValidatorTest extends UnitTestCase {
  use AssertHelperTrait;

  /**
   * @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, $this
      ->castSafeStrings($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, $this
      ->castSafeStrings($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, $this
      ->castSafeStrings($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',
      'To uninstall Forum, first delete all <a href="/path/to/vocabulary/overview"><em class="placeholder">Vocabulary label</em></a> terms',
    ];
    $reasons = $this->forumUninstallValidator
      ->validate($module);
    $this
      ->assertSame($expected, $this
      ->castSafeStrings($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',
      'To uninstall Forum, first delete all <em class="placeholder">Vocabulary label</em> terms',
    ];
    $reasons = $this->forumUninstallValidator
      ->validate($module);
    $this
      ->assertSame($expected, $this
      ->castSafeStrings($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 = [
      'To uninstall Forum, first delete all <a href="/path/to/vocabulary/overview"><em class="placeholder">Vocabulary label</em></a> terms',
    ];
    $reasons = $this->forumUninstallValidator
      ->validate($module);
    $this
      ->assertSame($expected, $this
      ->castSafeStrings($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 = [
      'To uninstall Forum, first delete all <em class="placeholder">Vocabulary label</em> terms',
    ];
    $reasons = $this->forumUninstallValidator
      ->validate($module);
    $this
      ->assertSame($expected, $this
      ->castSafeStrings($reasons));
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AssertHelperTrait::castSafeStrings protected function Casts MarkupInterface objects into strings.
ForumUninstallValidatorTest::$forumUninstallValidator protected property
ForumUninstallValidatorTest::setUp protected function Overrides UnitTestCase::setUp
ForumUninstallValidatorTest::testValidate public function @covers ::validate
ForumUninstallValidatorTest::testValidateHasForumNodes public function @covers ::validate
ForumUninstallValidatorTest::testValidateHasTermsForVocabularyAccess public function @covers ::validate
ForumUninstallValidatorTest::testValidateHasTermsForVocabularyNoAccess public function @covers ::validate
ForumUninstallValidatorTest::testValidateHasTermsForVocabularyWithNodesAccess public function @covers ::validate
ForumUninstallValidatorTest::testValidateHasTermsForVocabularyWithNodesNoAccess public function @covers ::validate
ForumUninstallValidatorTest::testValidateNotForum public function @covers ::validate
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root.
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName protected function Mocks a block with a block plugin.
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed in 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.