e724dc6b89c5c14c4a9b8e415ae65cb64daa6cd5
[yaffs-website] / web / core / modules / comment / tests / src / Unit / Entity / CommentLockTest.php
1 <?php
2
3 namespace Drupal\Tests\comment\Unit\Entity;
4
5 use Drupal\Core\DependencyInjection\ContainerBuilder;
6 use Drupal\Tests\UnitTestCase;
7 use Symfony\Component\HttpFoundation\Request;
8 use Symfony\Component\HttpFoundation\RequestStack;
9
10 /**
11  * Tests comment acquires and releases the right lock.
12  *
13  * @group comment
14  */
15 class CommentLockTest extends UnitTestCase {
16
17   /**
18    * Test the lock behavior.
19    */
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');
31     $cid = 2;
32     $lock_name = "comment:$cid:.00/";
33     $lock->expects($this->at(0))
34       ->method('acquire')
35       ->with($lock_name, 30)
36       ->will($this->returnValue(TRUE));
37     $lock->expects($this->at(1))
38       ->method('release')
39       ->with($lock_name);
40     $lock->expects($this->exactly(2))
41       ->method($this->anything());
42     $container->set('lock', $lock);
43
44     $cache_tag_invalidator = $this->getMock('Drupal\Core\Cache\CacheTagsInvalidator');
45     $container->set('cache_tags.invalidator', $cache_tag_invalidator);
46
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)
55       ->getMock();
56     $comment->expects($this->once())
57       ->method('isNew')
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())
69       ->method('getThread')
70       ->will($this->returnValue(''));
71
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);
79
80     $entity_type = $this->getMock('\Drupal\Core\Entity\EntityTypeInterface');
81     $comment->expects($this->any())
82       ->method('getEntityType')
83       ->will($this->returnValue($entity_type));
84     $comment->expects($this->at(1))
85       ->method('get')
86       ->with('status')
87       ->will($this->returnValue((object) ['value' => NULL]));
88     $storage = $this->getMock('Drupal\comment\CommentStorageInterface');
89
90     // preSave() should acquire the lock. (This is what's really being tested.)
91     $comment->preSave($storage);
92     // Release the acquired lock before exiting the test.
93     $comment->postSave($storage);
94   }
95
96 }