You are here

class SubscriptionControllerTest in Feeds 8.3

@coversDefaultClass \Drupal\feeds\Controller\SubscriptionController @group feeds

Hierarchy

Expanded class hierarchy of SubscriptionControllerTest

File

tests/src/Unit/Controller/SubscriptionControllerTest.php, line 16

Namespace

Drupal\Tests\feeds\Unit\Controller
View source
class SubscriptionControllerTest extends UnitTestCase {

  /**
   * The controller under test.
   *
   * @var \Drupal\feeds\Controller\SubscriptionController
   */
  protected $controller;

  /**
   * The entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * The feed entity.
   *
   * @var \Drupal\feeds\FeedInterface
   */
  protected $feed;

  /**
   * The HTTP request.
   *
   * @var \Symfony\Component\HttpFoundation\Request
   */
  protected $request;

  /**
   * The subscription entity.
   *
   * @var \Drupal\feeds\SubscriptionInterface
   */
  protected $subscription;

  /**
   * The key/value store.
   *
   * @var \Drupal\Core\KeyValueStore\KeyValueStoreExpirableInterface
   */
  protected $kv;

  /**
   * {@inheritdoc}
   */
  public function setUp() {
    $this->request = new Request();
    $this->request->query
      ->set('hub_mode', 'subscribe');
    $this->request->query
      ->set('hub_challenge', '1234');
    $this->request->query
      ->set('hub_topic', 'http://example.com');
    $this->feed = $this
      ->prophesize('Drupal\\feeds\\FeedInterface');
    $this->subscription = $this
      ->prophesize('Drupal\\feeds\\SubscriptionInterface');
    $this->subscription
      ->getTopic()
      ->willReturn('http://example.com');
    $this->subscription
      ->getState()
      ->willReturn('subscribing');
    $this->subscription
      ->setState(Argument::type('string'))
      ->willReturn(NULL);
    $this->subscription
      ->setLease(10)
      ->willReturn(TRUE);
    $this->subscription
      ->save()
      ->willReturn(NULL);
    $this->subscription
      ->id()
      ->willReturn(1);
    $this->subscription
      ->getToken()
      ->willReturn('valid_token');
    $subscription_storage = $this
      ->prophesize('Drupal\\Core\\Entity\\EntityStorageInterface');
    $subscription_storage
      ->load(1)
      ->willReturn($this->subscription
      ->reveal());
    $subscription_storage
      ->load(2)
      ->willReturn(FALSE);
    $feed_storage = $this
      ->prophesize('Drupal\\Core\\Entity\\EntityStorageInterface');
    $feed_storage
      ->load(1)
      ->willReturn($this->feed
      ->reveal());
    $this->entityTypeManager = $this
      ->prophesize('Drupal\\Core\\Entity\\EntityTypeManagerInterface');
    $this->entityTypeManager
      ->getStorage('feeds_subscription')
      ->willReturn($subscription_storage
      ->reveal());
    $this->entityTypeManager
      ->getStorage('feeds_feed')
      ->willReturn($feed_storage
      ->reveal());
    $this->kv = $this
      ->prophesize('Drupal\\Core\\KeyValueStore\\KeyValueStoreExpirableInterface');
    $kv_factory = $this
      ->prophesize('Drupal\\Core\\KeyValueStore\\KeyValueExpirableFactoryInterface');
    $kv_factory
      ->get('feeds_push_unsubscribe')
      ->willReturn($this->kv
      ->reveal());
    $this->controller = new SubscriptionController($kv_factory
      ->reveal(), $this->entityTypeManager
      ->reveal());
  }

  /**
   * @covers ::create
   * @covers ::__construct
   */
  public function testCreate() {
    $container = new ContainerBuilder();
    $container
      ->set('keyvalue.expirable', $this
      ->prophesize('Drupal\\Core\\KeyValueStore\\KeyValueExpirableFactoryInterface')
      ->reveal());
    $container
      ->set('entity_type.manager', $this->entityTypeManager
      ->reveal());
    $this
      ->assertInstanceOf('Drupal\\feeds\\Controller\\SubscriptionController', SubscriptionController::create($container));
  }

  /**
   * @covers ::subscribe
   * @covers ::handleSubscribe
   */
  public function testSubscribe() {
    $this->request->query
      ->set('hub_lease_seconds', 10);
    $response = $this->controller
      ->subscribe(1, 'valid_token', $this->request);
    $this
      ->assertSame('1234', $response
      ->getContent());
  }

  /**
   * @covers ::subscribe
   * @covers ::handleUnsubscribe
   */
  public function testUnsubscribe() {
    $this->request->query
      ->set('hub_mode', 'unsubscribe');
    $this->kv
      ->get('valid_token:1')
      ->willReturn(TRUE);
    $this->kv
      ->delete('valid_token:1')
      ->willReturn(TRUE);
    $response = $this->controller
      ->subscribe(1, 'valid_token', $this->request);
    $this
      ->assertSame('1234', $response
      ->getContent());
  }

  /**
   * @covers ::subscribe
   */
  public function testMissingChallenge() {
    $this->request->query
      ->set('hub_challenge', NULL);
    $this
      ->expectException(NotFoundHttpException::class);
    $this->controller
      ->subscribe(1, 'valid_token', $this->request);
  }

  /**
   * @covers ::subscribe
   */
  public function testMissingTopic() {
    $this->request->query
      ->set('hub_topic', NULL);
    $this
      ->expectException(NotFoundHttpException::class);
    $this->controller
      ->subscribe(1, 'valid_token', $this->request);
  }

  /**
   * @covers ::subscribe
   */
  public function testWrongMode() {
    $this->request->query
      ->set('hub_mode', 'woops');
    $this
      ->expectException(NotFoundHttpException::class);
    $this->controller
      ->subscribe(1, 'valid_token', $this->request);
  }

  /**
   * @covers ::handleSubscribe
   */
  public function testWrongToken() {
    $this
      ->expectException(NotFoundHttpException::class);
    $this->controller
      ->subscribe(1, 'not_valid_token', $this->request);
  }

  /**
   * @covers ::handleSubscribe
   */
  public function testMissingSubscription() {
    $this
      ->expectException(NotFoundHttpException::class);
    $this->controller
      ->subscribe(2, 'valid_token', $this->request);
  }

  /**
   * @covers ::handleSubscribe
   */
  public function testWrongTopic() {
    $this->request->query
      ->set('hub_topic', 'http://example.com/topic');
    $this
      ->expectException(NotFoundHttpException::class);
    $this->controller
      ->subscribe(1, 'valid_token', $this->request);
  }

  /**
   * @covers ::handleSubscribe
   */
  public function testSubscriptionInWrongState() {
    $this->subscription
      ->getState()
      ->willReturn('unsubscribed');
    $this
      ->expectException(NotFoundHttpException::class);
    $this->controller
      ->subscribe(1, 'valid_token', $this->request);
  }

  /**
   * @covers ::handleUnsubscribe
   */
  public function testSubscriptionMissingKv() {
    $this->request->query
      ->set('hub_mode', 'unsubscribe');
    $this->request->query
      ->set('hub_topic', 'http://example.com/topic');
    $this
      ->expectException(NotFoundHttpException::class);
    $this->controller
      ->subscribe(1, 'valid_token', $this->request);
  }

  /**
   * @covers ::receive
   */
  public function testReceive() {
    $payload = 'abcdefg';
    $sig = hash_hmac('sha1', $payload, 'secret');
    $request = new Request([], [], [], [], [], [], $payload);
    $request->headers
      ->set('X-Hub-Signature', 'sha1=' . $sig);
    $this->subscription
      ->checkSignature($sig, $payload)
      ->willReturn(TRUE);
    $response = $this->controller
      ->receive($this->subscription
      ->reveal(), 'valid_token', $request);
    $this
      ->assertSame(200, $response
      ->getStatusCode());
  }

  /**
   * @covers ::receive
   */
  public function testReceiveMissingSig() {
    $this
      ->expectException(NotFoundHttpException::class);
    $this->controller
      ->receive($this->subscription
      ->reveal(), 'valid_token', $this->request);
  }

  /**
   * @covers ::receive
   */
  public function testReceiveBadSig() {
    $payload = 'abcdefg';
    $sig = 'oops';
    $request = new Request([], [], [], [], [], [], $payload);
    $request->headers
      ->set('X-Hub-Signature', 'sha1=' . $sig);
    $this->subscription
      ->checkSignature($sig, $payload)
      ->willReturn(FALSE);
    $this
      ->expectException(NotFoundHttpException::class);
    $this->controller
      ->receive($this->subscription
      ->reveal(), 'valid_token', $request);
  }

  /**
   * @covers ::receive
   */
  public function testReceiveBadToken() {
    $this
      ->expectException(NotFoundHttpException::class);
    $this->controller
      ->receive($this->subscription
      ->reveal(), 'not_valid_token', $this->request);
  }

  /**
   * @covers ::receive
   */
  public function testReceiveFeedFailed() {
    $payload = 'abcdefg';
    $sig = hash_hmac('sha1', $payload, 'secret');
    $request = new Request([], [], [], [], [], [], $payload);
    $request->headers
      ->set('X-Hub-Signature', 'sha1=' . $sig);
    $this->subscription
      ->checkSignature($sig, $payload)
      ->willReturn(TRUE);
    $this->feed
      ->pushImport($payload)
      ->willThrow(new \Exception());
    $response = $this->controller
      ->receive($this->subscription
      ->reveal(), 'valid_token', $request);
    $this
      ->assertSame(500, $response
      ->getStatusCode());
  }

}

Members

Namesort descending Modifiers Type Description Overrides
PhpunitCompatibilityTrait::getMock Deprecated public function Returns a mock object for the specified class using the available method.
PhpunitCompatibilityTrait::setExpectedException Deprecated public function Compatibility layer for PHPUnit 6 to support PHPUnit 4 code.
SubscriptionControllerTest::$controller protected property The controller under test.
SubscriptionControllerTest::$entityTypeManager protected property The entity type manager.
SubscriptionControllerTest::$feed protected property The feed entity.
SubscriptionControllerTest::$kv protected property The key/value store.
SubscriptionControllerTest::$request protected property The HTTP request.
SubscriptionControllerTest::$subscription protected property The subscription entity.
SubscriptionControllerTest::setUp public function Overrides UnitTestCase::setUp
SubscriptionControllerTest::testCreate public function @covers ::create @covers ::__construct
SubscriptionControllerTest::testMissingChallenge public function @covers ::subscribe
SubscriptionControllerTest::testMissingSubscription public function @covers ::handleSubscribe
SubscriptionControllerTest::testMissingTopic public function @covers ::subscribe
SubscriptionControllerTest::testReceive public function @covers ::receive
SubscriptionControllerTest::testReceiveBadSig public function @covers ::receive
SubscriptionControllerTest::testReceiveBadToken public function @covers ::receive
SubscriptionControllerTest::testReceiveFeedFailed public function @covers ::receive
SubscriptionControllerTest::testReceiveMissingSig public function @covers ::receive
SubscriptionControllerTest::testSubscribe public function @covers ::subscribe @covers ::handleSubscribe
SubscriptionControllerTest::testSubscriptionInWrongState public function @covers ::handleSubscribe
SubscriptionControllerTest::testSubscriptionMissingKv public function @covers ::handleUnsubscribe
SubscriptionControllerTest::testUnsubscribe public function @covers ::subscribe @covers ::handleUnsubscribe
SubscriptionControllerTest::testWrongMode public function @covers ::subscribe
SubscriptionControllerTest::testWrongToken public function @covers ::handleSubscribe
SubscriptionControllerTest::testWrongTopic public function @covers ::handleSubscribe
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName Deprecated protected function Mocks a block with a block plugin. 1
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.