Newer
Older
catch
committed
<?php
/**
* @file
* Contains \Drupal\comment\Tests\Entity\CommentTest
*/
namespace Drupal\comment\Tests\Entity;
catch
committed
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\Entity\EntityType;
catch
committed
use Drupal\Tests\UnitTestCase;
/**
* Unit tests for the comment entity lock behavior.
*
* @group Drupal
* @group Comment
catch
committed
*/
class CommentLockTest extends UnitTestCase {
/**
* {@inheritdoc}
*/
public static function getInfo() {
return array(
'name' => 'Comment locks',
'description' => 'Test comment acquires and releases the right lock.',
'group' => 'Comment',
);
}
/**
* Test the lock behavior.
*/
public function testLocks() {
$container = new ContainerBuilder();
$container->set('module_handler', $this->getMock('Drupal\Core\Extension\ModuleHandlerInterface'));
catch
committed
$container->set('current_user', $this->getMock('Drupal\Core\Session\AccountInterface'));
Alex Pott
committed
$container->set('cache.test', $this->getMock('Drupal\Core\Cache\CacheBackendInterface'));
$container->setParameter('cache_bins', array('cache.test' => 'test'));
catch
committed
$container->register('request', 'Symfony\Component\HttpFoundation\Request');
$lock = $this->getMock('Drupal\Core\Lock\LockBackendInterface');
$cid = 2;
$lock_name = "comment:$cid:.00/";
catch
committed
$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);
\Drupal::setContainer($container);
$methods = get_class_methods('Drupal\comment\Entity\Comment');
unset($methods[array_search('preSave', $methods)]);
unset($methods[array_search('postSave', $methods)]);
$methods[] = 'onSaveOrDelete';
$methods[] = 'onUpdateBundleEntity';
catch
committed
$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(''));
$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) array('value' => NULL)));
Alex Pott
committed
$comment->expects($this->once())
->method('getCacheTag')
->will($this->returnValue(array('comment' => array($cid))));
$comment->expects($this->once())
->method('getListCacheTags')
->will($this->returnValue(array('comments' => TRUE)));
catch
committed
$storage = $this->getMock('Drupal\comment\CommentStorageInterface');
$comment->preSave($storage);
$comment->postSave($storage);