X-Git-Url: http://aleph1.co.uk/gitweb/?a=blobdiff_plain;ds=sidebyside;f=web%2Fcore%2Fmodules%2Fcomment%2Ftests%2Fsrc%2FUnit%2FEntity%2FCommentLockTest.php;fp=web%2Fcore%2Fmodules%2Fcomment%2Ftests%2Fsrc%2FUnit%2FEntity%2FCommentLockTest.php;h=e724dc6b89c5c14c4a9b8e415ae65cb64daa6cd5;hb=a2bd1bf0c2c1f1a17d188f4dc0726a45494cefae;hp=0000000000000000000000000000000000000000;hpb=57c063afa3f66b07c4bbddc2d6129a96d90f0aad;p=yaffs-website diff --git a/web/core/modules/comment/tests/src/Unit/Entity/CommentLockTest.php b/web/core/modules/comment/tests/src/Unit/Entity/CommentLockTest.php new file mode 100644 index 000000000..e724dc6b8 --- /dev/null +++ b/web/core/modules/comment/tests/src/Unit/Entity/CommentLockTest.php @@ -0,0 +1,96 @@ +set('module_handler', $this->getMock('Drupal\Core\Extension\ModuleHandlerInterface')); + $container->set('current_user', $this->getMock('Drupal\Core\Session\AccountInterface')); + $container->set('cache.test', $this->getMock('Drupal\Core\Cache\CacheBackendInterface')); + $container->set('comment.statistics', $this->getMock('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->getMock('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->getMock('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('')); + + $parent_entity = $this->getMock('\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->getMock('\Drupal\Core\Entity\EntityTypeInterface'); + $comment->expects($this->any()) + ->method('getEntityType') + ->will($this->returnValue($entity_type)); + $comment->expects($this->at(1)) + ->method('get') + ->with('status') + ->will($this->returnValue((object) ['value' => NULL])); + $storage = $this->getMock('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); + } + +}