3 namespace Drupal\Tests\comment\Unit\Entity;
5 use Drupal\Core\DependencyInjection\ContainerBuilder;
6 use Drupal\Tests\UnitTestCase;
7 use Symfony\Component\HttpFoundation\Request;
8 use Symfony\Component\HttpFoundation\RequestStack;
11 * Tests comment acquires and releases the right lock.
15 class CommentLockTest extends UnitTestCase {
18 * Test the lock behavior.
20 public function testLocks() {
21 $container = new ContainerBuilder();
22 $container->set('module_handler', $this->getMock('Drupal\Core\Extension\ModuleHandlerInterface'));
23 $container->set('current_user', $this->getMock('Drupal\Core\Session\AccountInterface'));
24 $container->set('cache.test', $this->getMock('Drupal\Core\Cache\CacheBackendInterface'));
25 $container->set('comment.statistics', $this->getMock('Drupal\comment\CommentStatisticsInterface'));
26 $request_stack = new RequestStack();
27 $request_stack->push(Request::create('/'));
28 $container->set('request_stack', $request_stack);
29 $container->setParameter('cache_bins', ['cache.test' => 'test']);
30 $lock = $this->getMock('Drupal\Core\Lock\LockBackendInterface');
32 $lock_name = "comment:$cid:.00/";
33 $lock->expects($this->at(0))
35 ->with($lock_name, 30)
36 ->will($this->returnValue(TRUE));
37 $lock->expects($this->at(1))
40 $lock->expects($this->exactly(2))
41 ->method($this->anything());
42 $container->set('lock', $lock);
44 $cache_tag_invalidator = $this->getMock('Drupal\Core\Cache\CacheTagsInvalidator');
45 $container->set('cache_tags.invalidator', $cache_tag_invalidator);
47 \Drupal::setContainer($container);
48 $methods = get_class_methods('Drupal\comment\Entity\Comment');
49 unset($methods[array_search('preSave', $methods)]);
50 unset($methods[array_search('postSave', $methods)]);
51 $methods[] = 'invalidateTagsOnSave';
52 $comment = $this->getMockBuilder('Drupal\comment\Entity\Comment')
53 ->disableOriginalConstructor()
54 ->setMethods($methods)
56 $comment->expects($this->once())
58 ->will($this->returnValue(TRUE));
59 $comment->expects($this->once())
60 ->method('hasParentComment')
61 ->will($this->returnValue(TRUE));
62 $comment->expects($this->once())
63 ->method('getParentComment')
64 ->will($this->returnValue($comment));
65 $comment->expects($this->once())
66 ->method('getCommentedEntityId')
67 ->will($this->returnValue($cid));
68 $comment->expects($this->any())
70 ->will($this->returnValue(''));
72 $parent_entity = $this->getMock('\Drupal\Core\Entity\ContentEntityInterface');
73 $parent_entity->expects($this->atLeastOnce())
74 ->method('getCacheTagsToInvalidate')
75 ->willReturn(['node:1']);
76 $comment->expects($this->once())
77 ->method('getCommentedEntity')
78 ->willReturn($parent_entity);
80 $entity_type = $this->getMock('\Drupal\Core\Entity\EntityTypeInterface');
81 $comment->expects($this->any())
82 ->method('getEntityType')
83 ->will($this->returnValue($entity_type));
84 $storage = $this->getMock('Drupal\comment\CommentStorageInterface');
86 // preSave() should acquire the lock. (This is what's really being tested.)
87 $comment->preSave($storage);
88 // Release the acquired lock before exiting the test.
89 $comment->postSave($storage);