You are here

class CommentLockTest in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/comment/tests/src/Unit/Entity/CommentLockTest.php \Drupal\Tests\comment\Unit\Entity\CommentLockTest
  2. 10 core/modules/comment/tests/src/Unit/Entity/CommentLockTest.php \Drupal\Tests\comment\Unit\Entity\CommentLockTest

Tests comment acquires and releases the right lock.

@group comment

Hierarchy

Expanded class hierarchy of CommentLockTest

File

core/modules/comment/tests/src/Unit/Entity/CommentLockTest.php, line 15

Namespace

Drupal\Tests\comment\Unit\Entity
View source
class CommentLockTest extends UnitTestCase {

  /**
   * Test the lock behavior.
   */
  public function testLocks() {
    $container = new ContainerBuilder();
    $container
      ->set('module_handler', $this
      ->createMock('Drupal\\Core\\Extension\\ModuleHandlerInterface'));
    $container
      ->set('current_user', $this
      ->createMock('Drupal\\Core\\Session\\AccountInterface'));
    $container
      ->set('cache.test', $this
      ->createMock('Drupal\\Core\\Cache\\CacheBackendInterface'));
    $container
      ->set('comment.statistics', $this
      ->createMock('Drupal\\comment\\CommentStatisticsInterface'));
    $request_stack = new RequestStack();
    $request_stack
      ->push(Request::create('/'));
    $container
      ->set('request_stack', $request_stack);
    $container
      ->setParameter('cache_bins', [
      'cache.test' => 'test',
    ]);
    $lock = $this
      ->createMock('Drupal\\Core\\Lock\\LockBackendInterface');
    $cid = 2;
    $lock_name = "comment:{$cid}:.00/";
    $lock
      ->expects($this
      ->at(0))
      ->method('acquire')
      ->with($lock_name, 30)
      ->will($this
      ->returnValue(TRUE));
    $lock
      ->expects($this
      ->at(1))
      ->method('release')
      ->with($lock_name);
    $lock
      ->expects($this
      ->exactly(2))
      ->method($this
      ->anything());
    $container
      ->set('lock', $lock);
    $cache_tag_invalidator = $this
      ->createMock('Drupal\\Core\\Cache\\CacheTagsInvalidator');
    $container
      ->set('cache_tags.invalidator', $cache_tag_invalidator);
    \Drupal::setContainer($container);
    $methods = get_class_methods('Drupal\\comment\\Entity\\Comment');
    unset($methods[array_search('preSave', $methods)]);
    unset($methods[array_search('postSave', $methods)]);
    $methods[] = 'invalidateTagsOnSave';
    $comment = $this
      ->getMockBuilder('Drupal\\comment\\Entity\\Comment')
      ->disableOriginalConstructor()
      ->setMethods($methods)
      ->getMock();
    $comment
      ->expects($this
      ->once())
      ->method('isNew')
      ->will($this
      ->returnValue(TRUE));
    $comment
      ->expects($this
      ->once())
      ->method('hasParentComment')
      ->will($this
      ->returnValue(TRUE));
    $comment
      ->expects($this
      ->once())
      ->method('getParentComment')
      ->will($this
      ->returnValue($comment));
    $comment
      ->expects($this
      ->once())
      ->method('getCommentedEntityId')
      ->will($this
      ->returnValue($cid));
    $comment
      ->expects($this
      ->any())
      ->method('getThread')
      ->will($this
      ->returnValue(''));
    $anon_user = $this
      ->createMock('Drupal\\Core\\Session\\AccountInterface');
    $anon_user
      ->expects($this
      ->any())
      ->method('isAnonymous')
      ->will($this
      ->returnValue(TRUE));
    $comment
      ->expects($this
      ->any())
      ->method('getOwner')
      ->will($this
      ->returnValue($anon_user));
    $parent_entity = $this
      ->createMock('\\Drupal\\Core\\Entity\\ContentEntityInterface');
    $parent_entity
      ->expects($this
      ->atLeastOnce())
      ->method('getCacheTagsToInvalidate')
      ->willReturn([
      'node:1',
    ]);
    $comment
      ->expects($this
      ->once())
      ->method('getCommentedEntity')
      ->willReturn($parent_entity);
    $entity_type = $this
      ->createMock('\\Drupal\\Core\\Entity\\EntityTypeInterface');
    $comment
      ->expects($this
      ->any())
      ->method('getEntityType')
      ->will($this
      ->returnValue($entity_type));
    $storage = $this
      ->createMock('Drupal\\comment\\CommentStorageInterface');

    // preSave() should acquire the lock. (This is what's really being tested.)
    $comment
      ->preSave($storage);

    // Release the acquired lock before exiting the test.
    $comment
      ->postSave($storage);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CommentLockTest::testLocks public function Test the lock behavior.
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.
UnitTestCase::setUp protected function 340