class SubscriptionControllerTest in Feeds 8.3
@coversDefaultClass \Drupal\feeds\Controller\SubscriptionController @group feeds
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait
- class \Drupal\Tests\feeds\Unit\Controller\SubscriptionControllerTest
Expanded class hierarchy of SubscriptionControllerTest
File
- tests/
src/ Unit/ Controller/ SubscriptionControllerTest.php, line 16
Namespace
Drupal\Tests\feeds\Unit\ControllerView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
PhpunitCompatibilityTrait:: |
public | function | Returns a mock object for the specified class using the available method. | |
PhpunitCompatibilityTrait:: |
public | function | Compatibility layer for PHPUnit 6 to support PHPUnit 4 code. | |
SubscriptionControllerTest:: |
protected | property | The controller under test. | |
SubscriptionControllerTest:: |
protected | property | The entity type manager. | |
SubscriptionControllerTest:: |
protected | property | The feed entity. | |
SubscriptionControllerTest:: |
protected | property | The key/value store. | |
SubscriptionControllerTest:: |
protected | property | The HTTP request. | |
SubscriptionControllerTest:: |
protected | property | The subscription entity. | |
SubscriptionControllerTest:: |
public | function |
Overrides UnitTestCase:: |
|
SubscriptionControllerTest:: |
public | function | @covers ::create @covers ::__construct | |
SubscriptionControllerTest:: |
public | function | @covers ::subscribe | |
SubscriptionControllerTest:: |
public | function | @covers ::handleSubscribe | |
SubscriptionControllerTest:: |
public | function | @covers ::subscribe | |
SubscriptionControllerTest:: |
public | function | @covers ::receive | |
SubscriptionControllerTest:: |
public | function | @covers ::receive | |
SubscriptionControllerTest:: |
public | function | @covers ::receive | |
SubscriptionControllerTest:: |
public | function | @covers ::receive | |
SubscriptionControllerTest:: |
public | function | @covers ::receive | |
SubscriptionControllerTest:: |
public | function | @covers ::subscribe @covers ::handleSubscribe | |
SubscriptionControllerTest:: |
public | function | @covers ::handleSubscribe | |
SubscriptionControllerTest:: |
public | function | @covers ::handleUnsubscribe | |
SubscriptionControllerTest:: |
public | function | @covers ::subscribe @covers ::handleUnsubscribe | |
SubscriptionControllerTest:: |
public | function | @covers ::subscribe | |
SubscriptionControllerTest:: |
public | function | @covers ::handleSubscribe | |
SubscriptionControllerTest:: |
public | function | @covers ::handleSubscribe | |
UnitTestCase:: |
protected | property | The random generator. | |
UnitTestCase:: |
protected | property | The app root. | 1 |
UnitTestCase:: |
protected | function | Asserts if two arrays are equal by sorting them first. | |
UnitTestCase:: |
protected | function | Mocks a block with a block plugin. | 1 |
UnitTestCase:: |
protected | function | Returns a stub class resolver. | |
UnitTestCase:: |
public | function | Returns a stub config factory that behaves according to the passed array. | |
UnitTestCase:: |
public | function | Returns a stub config storage that returns the supplied configuration. | |
UnitTestCase:: |
protected | function | Sets up a container with a cache tags invalidator. | |
UnitTestCase:: |
protected | function | Gets the random generator for the utility methods. | |
UnitTestCase:: |
public | function | Returns a stub translation manager that just returns the passed string. | |
UnitTestCase:: |
public | function | Generates a unique random string containing letters and numbers. |