View source
<?php
namespace Drupal\Tests\acquia_contenthub_subscriber\Functional;
use Drupal\acquia_contenthub_subscriber\ContentHubFilterInterface;
class ContentHubFiltersTest extends ContentHubSubscriberTestBase {
public function testFiltersAdminUser() {
$this
->drupalLogin($this->adminUser);
$filter = $this
->entityCreate('contenthub_filter', $this->adminUser);
$filter
->save();
$this
->getHttpRequest($filter, TRUE);
$this
->getHttpRequest($filter, FALSE);
$filter = $this
->createContentHubFilter();
$this
->postHttpRequest($filter);
$this
->patchHttpRequest($filter);
$this
->deleteHttpRequest($filter);
$this
->drupalLogout();
}
public function testFiltersUnAuthorizedUser() {
$this
->drupalLogin($this->unauthorizedUser);
$this
->allHttpRequestDenied();
$this
->drupalLogout();
}
public function testFiltersAnonymousUser() {
$this
->drupalLogin($this->anonymousUser);
$this
->allHttpRequestDenied();
$this
->drupalLogout();
}
protected function getContentHubFilterResourceUrl($method, $id = 'all') {
$url = '/acquia_contenthub/contenthub_filter';
switch ($method) {
case 'GET':
case 'PATCH':
case 'DELETE':
$url .= "/{$id}";
return $url;
case 'POST':
return $url;
}
}
protected function createContentHubFilter() {
$data = $this
->entityValues('contenthub_filter');
return $data;
}
protected function getHttpRequest(ContentHubFilterInterface $filter, $multi = FALSE) {
$serialized = $this->container
->get('serializer')
->serialize([
$filter,
], 'json');
$method = 'GET';
$id = $multi ? 'all' : $filter
->id();
$url = $this
->getContentHubFilterResourceUrl($method, $id);
$entities = $this
->httpRequest($url, $method, NULL, 'application/json');
$this
->assertEquals($entities, $serialized);
}
protected function postHttpRequest(array $filter) {
$serialized = $this->container
->get('serializer')
->serialize($filter, 'json');
$method = 'POST';
$url = $this
->getContentHubFilterResourceUrl($method);
$entity_json = $this
->httpRequest($url, $method, $serialized, 'application/json');
$entity = $this->entityConfigStorage
->load($filter['id']);
$serialized = $this->container
->get('serializer')
->serialize($entity, 'json');
$this
->assertEquals($entity_json, $serialized);
}
protected function patchHttpRequest(array $saved_filter) {
$filter = $saved_filter;
$filter['name'] = $saved_filter['name'] . ' - updated';
$serialized = $this->container
->get('serializer')
->serialize($filter, 'json');
$method = 'PATCH';
$url = $this
->getContentHubFilterResourceUrl($method, $filter['id']);
$entity_json = $this
->httpRequest($url, $method, $serialized, 'application/json');
$entity = $this->entityConfigStorage
->load($filter['id']);
$serialized = $this->container
->get('serializer')
->serialize($entity, 'json');
$this
->assertFalse($entity->name === $saved_filter['name']);
$this
->assertEquals($entity_json, $serialized);
}
protected function deleteHttpRequest(array $filter) {
$method = 'DELETE';
$url = $this
->getContentHubFilterResourceUrl($method, $filter['id']);
$this
->httpRequest($url, $method, NULL, 'application/json');
$this
->assertResponse(204);
$entity = $this->entityConfigStorage
->load($filter['id']);
$this
->assertNull($entity);
}
protected function allHttpRequestDenied() {
$saved_filter = $this
->entityCreate('contenthub_filter', $this->adminUser);
$saved_filter
->save();
$method = 'GET';
$url = $this
->getContentHubFilterResourceUrl($method);
$this
->httpRequest($url, $method, NULL, 'application/json');
$this
->assertSession()
->statusCodeEquals(403);
$method = 'GET';
$url = $this
->getContentHubFilterResourceUrl($method, $saved_filter
->id());
$this
->httpRequest($url, $method, NULL, 'application/json');
$this
->assertSession()
->statusCodeEquals(403);
$filter = $this
->createContentHubFilter();
$method = 'POST';
$url = $this
->getContentHubFilterResourceUrl($method);
$serialized = $this->container
->get('serializer')
->serialize($filter, 'json');
$this
->httpRequest($url, $method, $serialized, 'application/json');
$this
->assertSession()
->statusCodeEquals(403);
$method = 'PATCH';
$url = $this
->getContentHubFilterResourceUrl($method, $saved_filter
->id());
$save_filter = $saved_filter;
$save_filter->name .= '- Updated';
$serialized = $this->container
->get('serializer')
->serialize($save_filter, 'json');
$this
->httpRequest($url, $method, $serialized, 'application/json');
$this
->assertSession()
->statusCodeEquals(403);
$method = 'DELETE';
$url = $this
->getContentHubFilterResourceUrl($method, $saved_filter
->id());
$this
->httpRequest($url, $method, NULL, 'application/json');
$this
->assertSession()
->statusCodeEquals(403);
}
}