CommentLockTest.php 3.45 KB
Newer Older
1 2 3
<?php
/**
 * @file
4
 * Contains \Drupal\Tests\comment\Unit\Entity\CommentTest
5 6
 */

7
namespace Drupal\Tests\comment\Unit\Entity;
8 9

use Drupal\Core\DependencyInjection\ContainerBuilder;
10
use Drupal\Core\Entity\EntityType;
11
use Drupal\Tests\UnitTestCase;
12 13
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
14 15

/**
16
 * Tests comment acquires and releases the right lock.
17
 *
18
 * @group comment
19 20 21 22 23 24 25 26
 */
class CommentLockTest extends UnitTestCase {

  /**
   * Test the lock behavior.
   */
  public function testLocks() {
    $container = new ContainerBuilder();
27
    $container->set('module_handler', $this->getMock('Drupal\Core\Extension\ModuleHandlerInterface'));
28
    $container->set('current_user', $this->getMock('Drupal\Core\Session\AccountInterface'));
29
    $container->set('cache.test', $this->getMock('Drupal\Core\Cache\CacheBackendInterface'));
30
    $container->set('comment.statistics', $this->getMock('Drupal\comment\CommentStatisticsInterface'));
31 32 33
    $request_stack = new RequestStack();
    $request_stack->push(Request::create('/'));
    $container->set('request_stack', $request_stack);
34
    $container->setParameter('cache_bins', array('cache.test' => 'test'));
35 36
    $lock = $this->getMock('Drupal\Core\Lock\LockBackendInterface');
    $cid = 2;
37
    $lock_name = "comment:$cid:.00/";
38 39 40 41 42 43 44 45 46 47 48 49 50 51
    $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)]);
52
    $methods[] = 'onSaveOrDelete';
53
    $methods[] = 'onUpdateBundleEntity';
54 55 56 57 58 59 60
    $comment = $this->getMockBuilder('Drupal\comment\Entity\Comment')
      ->disableOriginalConstructor()
      ->setMethods($methods)
      ->getMock();
    $comment->expects($this->once())
      ->method('isNew')
      ->will($this->returnValue(TRUE));
61 62 63 64 65 66 67 68 69 70 71 72
    $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(''));
73 74 75 76 77 78

    $entity_type = $this->getMock('\Drupal\Core\Entity\EntityTypeInterface');
    $comment->expects($this->any())
      ->method('getEntityType')
      ->will($this->returnValue($entity_type));
    $comment->expects($this->at(1))
79 80 81
      ->method('get')
      ->with('status')
      ->will($this->returnValue((object) array('value' => NULL)));
82 83 84 85 86 87
    $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)));
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
  }

}