Skip to content

Commit

Permalink
Repair button change request
Browse files Browse the repository at this point in the history
* Set text to publish of a new or copied entity

* Translate entity button publish and change

* Add logic based on the entity status

* Mock partial to test isRequestedProductionEntity

* Update CHANGELOG.md

see: https://www.pivotaltracker.com/story/show/184302283
  • Loading branch information
Shaky212 committed Jan 30, 2023
1 parent 362ceaa commit 9662d06
Show file tree
Hide file tree
Showing 12 changed files with 258 additions and 32 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
## 4.4.2
**Bugfix**
- Repair button change request #575

## 4.4.1
**Backport**
- Include 4.3.2 into the 4.4 tier of releases (More Label and translation adjustments #574)

## 4.4.0
**Feature**
- Added the IdP connection request feature for production entities #555
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
* entity is used in a specific context. This particularly applies for the factory
* methods found in the 'Entity/Entity' namespace.
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
* @SuppressWarnings(PHPMD.ExcessiveClassComplexity) This object contains a lot of properties which cannot not be easily
* simplified.
*/
class ManageEntity
{
Expand Down Expand Up @@ -350,4 +352,15 @@ public function asArray(): array
$data += $this->protocol->asArray();
return $data;
}

private function isStatusPublicationRequested()
{
return $this->getStatus() === Constants::STATE_PUBLICATION_REQUESTED;
}

public function isRequestedProductionEntity(
bool $isCopy
) {
return $isCopy || ($this->isStatusPublicationRequested() && $this->isProduction());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,26 +70,26 @@ public function createCreateForm(string $type, Service $service, string $environ
case ($type === Constants::TYPE_SAML):
$command = SaveSamlEntityCommand::forCreateAction($service);
$command->setEnvironment($environment);
return $this->formFactory->create(SamlEntityType::class, $command, $this->createBuildOptions($environment));
return $this->formFactory->create(SamlEntityType::class, $command, $this->requestedBuildOptions($environment));
case ($type === Constants::TYPE_OPENID_CONNECT_TNG):
$command = SaveOidcngEntityCommand::forCreateAction($service);
$command->setEnvironment($environment);
return $this->formFactory->create(OidcngEntityType::class, $command, $this->createBuildOptions($environment));
return $this->formFactory->create(OidcngEntityType::class, $command, $this->requestedBuildOptions($environment));
case ($type === Constants::TYPE_OPENID_CONNECT_TNG_RESOURCE_SERVER):
$command = SaveOidcngResourceServerEntityCommand::forCreateAction($service);
$command->setEnvironment($environment);
return $this->formFactory->create(
OidcngResourceServerEntityType::class,
$command,
$this->createBuildOptions($environment)
$this->requestedBuildOptions($environment)
);
case ($type === Constants::TYPE_OAUTH_CLIENT_CREDENTIAL_CLIENT):
$command = SaveOauthClientCredentialClientCommand::forCreateAction($service);
$command->setEnvironment($environment);
return $this->formFactory->create(
OauthClientCredentialEntityType::class,
$command,
$this->createBuildOptions($environment)
$this->requestedBuildOptions($environment)
);
}

Expand All @@ -98,15 +98,18 @@ public function createCreateForm(string $type, Service $service, string $environ

public function createEditForm(ManageEntity $entity, Service $service, string $environment, $isCopy = false)
{
$buildOptions = $entity->isRequestedProductionEntity($isCopy) ?
$this->requestedBuildOptions($environment) : $this->publishedBuildOptions($environment);

switch ($entity->getProtocol()->getProtocol()) {
case (Constants::TYPE_SAML):
$command = $this->saveCommandFactory->buildSamlCommandByManageEntity($entity, $environment);
$command->setService($service);
return $this->formFactory->create(SamlEntityType::class, $command, $this->editBuildOptions($environment));
return $this->formFactory->create(SamlEntityType::class, $command, $buildOptions);
case (Constants::TYPE_OPENID_CONNECT_TNG):
$command = $this->saveCommandFactory->buildOidcngCommandByManageEntity($entity, $environment, $isCopy);
$command->setService($service);
return $this->formFactory->create(OidcngEntityType::class, $command, $this->editBuildOptions($environment));
return $this->formFactory->create(OidcngEntityType::class, $command, $buildOptions);
case (Constants::TYPE_OPENID_CONNECT_TNG_RESOURCE_SERVER):
$command = $this
->saveCommandFactory
Expand All @@ -119,18 +122,18 @@ public function createEditForm(ManageEntity $entity, Service $service, string $e
return $this->formFactory->create(
OidcngResourceServerEntityType::class,
$command,
$this->editBuildOptions($environment)
$buildOptions
);
case (Constants::TYPE_OAUTH_CLIENT_CREDENTIAL_CLIENT):
$command = $this->saveCommandFactory->buildOauthCccCommandByManageEntity($entity, $environment, $isCopy);
$command->setService($service);
return $this->formFactory->create(OauthClientCredentialEntityType::class, $command, $this->editBuildOptions($environment));
return $this->formFactory->create(OauthClientCredentialEntityType::class, $command, $buildOptions);
}

throw new InvalidArgumentException("invalid form type requested");
}

private function createBuildOptions($environment)
private function requestedBuildOptions($environment)
{
$options = [];
if ($environment === Constants::ENVIRONMENT_PRODUCTION) {
Expand All @@ -139,13 +142,13 @@ private function createBuildOptions($environment)
return $options;
}

private function editBuildOptions($environment)
private function publishedBuildOptions($environment)
{
$options = [];
if ($environment === Constants::ENVIRONMENT_PRODUCTION) {
$options = ['validation_groups' => ['Default', 'production'],
'publish_button_label' => 'Change request',
];
'publish_button_label' => 'entity.edit.label.change',
];
}
return $options;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => SaveOauthClientCredentialClientCommand::class,
'publish_button_label' => 'Publish',
'publish_button_label' => 'entity.add.label.publish',
));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => SaveOidcngEntityCommand::class,
'publish_button_label' => 'Publish',
'publish_button_label' => 'entity.add.label.publish',
));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => SaveOidcngResourceServerEntityCommand::class,
'publish_button_label' => 'Publish',
'publish_button_label' => 'entity.add.label.publish',
));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => SaveSamlEntityCommand::class,
'publish_button_label' => 'Publish',
'publish_button_label' => 'entity.add.label.publish',
));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,8 @@ entity.edit.label.implicit: Implicit
entity.edit.label.authorization_code_token: response_type=id_token
entity.edit.label.implicit_id_token_token: response_type=id_token token
entity.edit.label.implicit_id_token: response_type=id_token
entity.edit.label.change: Change
entity.add.label.publish: Publish
entity.acl.title: Entity Idp access
entity.acl.info.title: Info
entity.acl.info.html: "
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
use Surfnet\ServiceProviderDashboard\Application\Command\Entity\SaveSamlEntityCommand;
use Surfnet\ServiceProviderDashboard\Application\Service\AttributeServiceInterface;
use Surfnet\ServiceProviderDashboard\Domain\Entity\Constants;
use Surfnet\ServiceProviderDashboard\Domain\Entity\ManageEntity;
use Surfnet\ServiceProviderDashboard\Domain\Entity\Service;
use Surfnet\ServiceProviderDashboard\Infrastructure\DashboardBundle\Factory\EntityTypeFactory;
use Surfnet\ServiceProviderDashboard\Infrastructure\DashboardBundle\Form\Entity\OidcngEntityType;
Expand Down Expand Up @@ -62,7 +63,6 @@ class EntityTypeFactoryTest extends MockeryTestCase

protected function setUp()
{

$this->formFactory = m::mock(FormFactory::class);
$this->saveCommandFactory = m::mock(SaveCommandFactoryInterface::class);
$this->service = m::mock(Service::class);
Expand Down Expand Up @@ -164,6 +164,7 @@ public function test_build_create_new_oidcng_form()
$this->assertInstanceOf(FormType::class, $form);
}


public function test_build_create_new_oidcng_form_from_entity()
{
$this->formFactory
Expand Down Expand Up @@ -200,20 +201,20 @@ public function test_build_create_new_oauth20_rs_form()
$this->formFactory
->shouldReceive('create')
->with(\Mockery::on(function ($entityType) {
$this->assertSame(OidcngResourceServerEntityType::class, $entityType);
return true;
}), \Mockery::on(function ($command) {
$this->assertInstanceOf(SaveOidcngResourceServerEntityCommand::class, $command);
return true;
}), \Mockery::on(function ($options) {
$this->assertSame([
'validation_groups' => [
0 => 'Default',
1 => 'production',
],
], $options);
return true;
}))
$this->assertSame(OidcngResourceServerEntityType::class, $entityType);
return true;
}), \Mockery::on(function ($command) {
$this->assertInstanceOf(SaveOidcngResourceServerEntityCommand::class, $command);
return true;
}), \Mockery::on(function ($options) {
$this->assertSame([
'validation_groups' => [
0 => 'Default',
1 => 'production',
],
], $options);
return true;
}))
->once()
->andReturn($this->form);

Expand All @@ -225,4 +226,112 @@ public function test_build_create_new_oauth20_rs_form()

$this->assertInstanceOf(FormType::class, $form);
}

/**
* @dataProvider providePublishOrChangeOptions
*/
public function test_build_edit_saml_form_from_entity(
$isCopy,
$manageEntityStatus,
$environment,
$expectation
) {
$manageEntity = m::mock(ManageEntity::class);

/**
* Make ManageEntity partial to test method isRequestedProductionEntity
*/
$manageEntity->makePartial();

$manageEntity
->shouldReceive('getProtocol->getProtocol')
->andReturn(Constants::TYPE_SAML);

$manageEntity
->shouldReceive('getEnvironment')
->andReturn($environment);

$manageEntity
->shouldReceive('getStatus')
->andReturn($manageEntityStatus);

$saveSamlEntityCommand = m::mock(SaveSamlEntityCommand::class);
$saveSamlEntityCommand
->shouldReceive('setService');

$this->saveCommandFactory
->shouldReceive('buildSamlCommandByManageEntity')
->with($manageEntity, $environment)
->andReturn($saveSamlEntityCommand);

$this->formFactory
->shouldReceive('create')
->with(\Mockery::on(function ($entityType) {
static::assertSame(SamlEntityType::class, $entityType);
return true;
}), \Mockery::on(function ($command) {
static::assertInstanceOf(SaveSamlEntityCommand::class, $command);
return true;
}), \Mockery::on(function ($options) use ($expectation) {
static::assertSame($expectation, $options);
return true;
}))
->once()
->andReturn($this->form);

$form = $this->factory->createEditForm(
$manageEntity,
$this->service,
$environment,
$isCopy
);

$this->assertInstanceOf(FormType::class, $form);
}

public static function providePublishOrChangeOptions(): array
{
return [
'new entity' => [
false,
Constants::STATE_PUBLICATION_REQUESTED,
Constants::ENVIRONMENT_PRODUCTION,
[
'validation_groups' => ['Default', 'production'],
]
],
'new entity copy' => [
true,
Constants::STATE_PUBLICATION_REQUESTED,
Constants::ENVIRONMENT_PRODUCTION,
[
'validation_groups' => ['Default', 'production'],
]
],
'existing published entity' => [
false,
Constants::STATE_PUBLISHED,
Constants::ENVIRONMENT_PRODUCTION,
[
'validation_groups' => ['Default', 'production'],
'publish_button_label' => 'entity.edit.label.change',
]
],
'copy entity ignore state and environment' => [
true,
Constants::STATE_PUBLISHED,
Constants::ENVIRONMENT_PRODUCTION,
[
'validation_groups' => ['Default', 'production'],
]
],
'new test entity' => [
false,
Constants::STATE_PUBLISHED,
Constants::ENVIRONMENT_TEST,
[
]
],
];
}
}
15 changes: 15 additions & 0 deletions tests/unit/Infrastructure/Manage/Dto/ManageEntityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
namespace Surfnet\ServiceProviderDashboard\Tests\Unit\Infrastructure\DashboardBundle\Manage\Dto;

use Mockery\Adapter\Phpunit\MockeryTestCase;
use Surfnet\ServiceProviderDashboard\Domain\Entity\Constants;
use Surfnet\ServiceProviderDashboard\Domain\Entity\Entity\Contact;
use Surfnet\ServiceProviderDashboard\Domain\Entity\ManageEntity;

Expand Down Expand Up @@ -102,4 +103,18 @@ public function test_diff_oidc()
// 'provide everything' manner.
$this->assertCount(2, $diffResults['metaDataFields.redirectUrls']);
}

public function test_is_status_publication_requested()
{
$entity = ManageEntity::fromApiResponse(json_decode(file_get_contents(__DIR__ . '/fixture/saml20_sp_requested.json'), true));
$entity->updateStatus(Constants::STATE_PUBLICATION_REQUESTED);
$this->assertEquals(Constants::STATE_PUBLICATION_REQUESTED, $entity->getStatus());
}

public function test_is_requested_production_entity_copy()
{
$entity = ManageEntity::fromApiResponse(json_decode(file_get_contents(__DIR__ . '/fixture/saml20_sp_requested.json'), true));
$this->assertTrue($entity->isRequestedProductionEntity(true));
$this->assertFalse($entity->isRequestedProductionEntity(false));
}
}
Loading

0 comments on commit 9662d06

Please sign in to comment.