View source
<?php
namespace Drupal\Tests\apigee_edge\Kernel;
use Drupal\apigee_edge\Entity\Developer;
use Drupal\Core\Form\FormState;
use Drupal\KernelTests\KernelTestBase;
use Drupal\Tests\apigee_mock_api_client\Traits\ApigeeMockApiClientHelperTrait;
use Drupal\Tests\user\Traits\UserCreationTrait;
use GuzzleHttp\Psr7\Response;
use Http\Message\Authentication\AutoBasicAuth;
class TestFrameworkKernelTest extends KernelTestBase {
use ApigeeMockApiClientHelperTrait;
use UserCreationTrait;
protected static $mock_api_client_ready = TRUE;
protected static $modules = [
'user',
'system',
'key',
'file',
'entity',
'syslog',
'apigee_edge',
'apigee_mock_api_client',
];
protected $developers = [];
protected function setUp() {
parent::setUp();
$this
->installConfig([
'apigee_edge',
]);
$this
->installEntitySchema('user');
$this
->installSchema('system', [
'sequences',
]);
$this
->installSchema('user', [
'users_data',
]);
$this
->apigeeTestHelperSetup();
}
public function testServiceModification() {
self::assertEquals((string) $this->container
->getDefinition('apigee_edge.sdk_connector')
->getArgument(0), 'apigee_mock_api_client.mock_http_client_factory');
}
public function testInlineResponseQueue() {
if ($this->integration_enabled) {
static::markTestSkipped('Only test the response queue when running offline tests.');
return;
}
$this->stack
->addResponse(new Response(200, [], "{\"status\": \"success\"}"));
$response = $this->sdkConnector
->buildClient(new AutoBasicAuth())
->get('/');
self::assertEquals("200", $response
->getStatusCode());
self::assertEquals('{"status": "success"}', (string) $response
->getBody());
}
public function testMatchedResponse() {
if ($this->integration_enabled) {
$this
->markTestSkipped('Integration enabled, skipping test.');
}
$org_name = $this
->randomMachineName();
$this
->addOrganizationMatchedResponse($org_name);
$org_controller = $this->container
->get('apigee_edge.controller.organization');
$org = $org_controller
->load($org_name);
$this
->assertEqual($org
->getName(), $org_name);
}
public function testStackedMockResponse() {
if ($this->integration_enabled) {
$this
->markTestSkipped('Integration enabled, skipping test.');
}
$test_user = [
'mail' => $this
->randomMachineName() . '@example.com',
'name' => $this
->randomMachineName(),
'first_name' => $this
->getRandomGenerator()
->word(16),
'last_name' => $this
->getRandomGenerator()
->word(16),
];
$account = $this->entityTypeManager
->getStorage('user')
->create($test_user);
$this
->assertEquals($test_user['mail'], $account->mail->value);
$this
->queueDeveloperResponse($account);
$developerStorage = $this->entityTypeManager
->getStorage('developer');
$developerStorage
->resetCache([
$test_user['mail'],
]);
$developer = $developerStorage
->load($test_user['mail']);
$this
->assertEqual($developer
->getEmail(), $test_user['mail']);
$this
->assertEqual($developer
->getAttributeValue('IS_MOCK_CLIENT'), 1);
}
public function testNotStackedMockResponse() {
if (!$this->integration_enabled) {
$this
->markTestSkipped('Integration not enabled, skipping test.');
}
$developerStorage = $this->entityTypeManager
->getStorage('developer');
$developer = $developerStorage
->create([
'email' => $this
->randomMachineName() . '@example.com',
'userName' => $this
->randomMachineName(),
'firstName' => $this
->getRandomGenerator()
->word(8),
'lastName' => $this
->getRandomGenerator()
->word(8),
]);
$developerStorage
->resetCache([
$developer
->getEmail(),
]);
$this
->queueDeveloperResponseFromDeveloper($developer);
$loaded_developer = $developerStorage
->load($developer
->getEmail());
$this
->isEmpty($loaded_developer);
$this
->queueDeveloperResponseFromDeveloper($developer, 201);
$developer
->save();
$this->developers[] = $developer;
$this
->queueDeveloperResponseFromDeveloper($developer);
$loaded_developer = $developerStorage
->load($developer
->getEmail());
$this
->assertInstanceOf(Developer::class, $loaded_developer);
$this
->assertEqual($loaded_developer
->getEmail(), $developer
->getEmail());
$this
->assertEmpty($developer
->getAttributeValue('IS_MOCK_CLIENT'));
}
public function testRegisterUser() {
if ($this->integration_enabled) {
$this
->markTestSkipped('Integration enabled, skipping test.');
}
$this
->addOrganizationMatchedResponse();
$this
->setUpCurrentUser([
'uid' => 0,
]);
$test_user = [
'mail' => $this
->randomMachineName() . '@example.com',
'name' => $this
->randomMachineName(),
'first_name' => $this
->getRandomGenerator()
->word(16),
'last_name' => $this
->getRandomGenerator()
->word(16),
];
$form_data = [
'mail' => $test_user['mail'],
'name' => $test_user['name'],
'first_name[0][value]' => $test_user['first_name'],
'last_name[0][value]' => $test_user['last_name'],
'op' => 'Create new account',
'pass' => $this
->getRandomGenerator()
->word(8),
];
$account = $this->entityTypeManager
->getStorage('user')
->create($test_user);
$formObject = $this->entityTypeManager
->getFormObject('user', 'register')
->setEntity($account);
$form_state = new FormState();
$form_state
->setUserInput($form_data);
$form_state
->setValues($form_data);
$this->stack
->queueMockResponse('get_not_found');
$this
->queueDeveloperResponse($account);
\Drupal::formBuilder()
->submitForm($formObject, $form_state);
$developerStorage = $this->entityTypeManager
->getStorage('developer');
$developerStorage
->resetCache([
$test_user['mail'],
]);
$developer = $developerStorage
->load($test_user['mail']);
$this
->assertEqual($developer
->getEmail(), $test_user['mail']);
$this
->assertEqual($developer
->getAttributeValue('IS_MOCK_CLIENT'), 1);
}
protected function tearDown() {
if ($this->integration_enabled && !empty($this->developers)) {
foreach ($this->developers as $developer) {
$developer
->delete();
}
}
parent::tearDown();
}
}