Skip to content
Snippets Groups Projects
Commit 020ba0b6 authored by Eirik Morland's avatar Eirik Morland
Browse files

Issue #3419751 by eiriksm: Add support for team limit override option

parent 47969f52
No related branches found
No related tags found
1 merge request!29Test all things related to team limits
Pipeline #89658 passed
......@@ -15,6 +15,7 @@ class TeamNode extends Node {
const ADMIN_FIELD = 'field_team_admins';
const PLAN_FIELD = 'field_plan';
const PROJECT_LIMIT_FIELD = 'field_project_limit';
const PROJECT_LIMIT_OVERRIDE_FIELD = 'field_team_allowed_override';
const SLACK_NOTIFICATION_ENABLED_FIELD = 'field_slack_notifications';
const BILLING_ENABLED_FIELD = 'field_send_receipts';
const BILLING_EMAIL_FIELD = 'field_billing_email';
......@@ -119,11 +120,45 @@ class TeamNode extends Node {
* Get the max private repos a team can have, based on the field value.
*/
public function getMaxPrivateRepos() : int {
$return = $this->getMaxPrivateReposWithoutOverride();
if ($this->hasProjectOverrideLimit()) {
$return = $this->getProjectOverrideLimit();
}
return $return;
}
/**
* Get the max private repos a team can have, based on the field value.
*
* This will discard the override field.
*/
public function getMaxPrivateReposWithoutOverride() : int {
$return = 0;
if (!$this->hasField(self::PROJECT_LIMIT_FIELD) || $this->get(self::PROJECT_LIMIT_FIELD)->isEmpty()) {
return $return;
}
return (int) $this->get(self::PROJECT_LIMIT_FIELD)->first()->getString();
$return = (int) $this->get(self::PROJECT_LIMIT_FIELD)->first()->getString();
return $return;
}
/**
* Check if the team has a project override limit.
*/
public function hasProjectOverrideLimit() : bool {
if (!$this->hasField(self::PROJECT_LIMIT_OVERRIDE_FIELD) || $this->get(self::PROJECT_LIMIT_OVERRIDE_FIELD)->isEmpty()) {
return FALSE;
}
return (bool) $this->get(self::PROJECT_LIMIT_OVERRIDE_FIELD)->first()->getString();
}
/**
* Get the project override limit.
*/
public function getProjectOverrideLimit() : int {
if (!$this->hasProjectOverrideLimit()) {
return 0;
}
return (int) $this->get(self::PROJECT_LIMIT_OVERRIDE_FIELD)->first()->getString();
}
/**
......@@ -136,6 +171,16 @@ class TeamNode extends Node {
$this->set(self::PROJECT_LIMIT_FIELD, $number);
}
/**
* Setter for max private repos.
*/
public function setMaxPrivateReposOverride($number) {
if (!$this->hasField(self::PROJECT_LIMIT_OVERRIDE_FIELD)) {
return;
}
$this->set(self::PROJECT_LIMIT_OVERRIDE_FIELD, $number);
}
/**
* Setter for the plan.
*/
......
......@@ -178,6 +178,54 @@ abstract class KernelTestBase extends CoreKernelTestBase {
]);
$field->save();
// Project limits.
$fieldStorage = FieldStorageConfig::create([
'field_name' => 'field_project_limit',
'entity_type' => 'node',
'type' => 'integer',
'cardinality' => 1,
]);
$fieldStorage->save();
$field = FieldConfig::create([
'field_storage' => $fieldStorage,
'bundle' => $this->nodeType->id(),
'field_type' => 'integer',
'required' => FALSE,
]);
$field->save();
// Projects override.
$fieldStorage = FieldStorageConfig::create([
'field_name' => 'field_team_allowed_override',
'entity_type' => 'node',
'type' => 'integer',
'cardinality' => 1,
]);
$fieldStorage->save();
$field = FieldConfig::create([
'field_storage' => $fieldStorage,
'bundle' => $this->nodeType->id(),
'field_type' => 'integer',
'required' => FALSE,
]);
$field->save();
// Plan field.
$fieldStorage = FieldStorageConfig::create([
'field_name' => 'field_plan',
'entity_type' => 'node',
'type' => 'string',
'cardinality' => 1,
]);
$fieldStorage->save();
$field = FieldConfig::create([
'field_storage' => $fieldStorage,
'bundle' => $this->nodeType->id(),
'field_type' => 'string',
'required' => FALSE,
]);
$field->save();
// Billing email.
$fieldStorage = FieldStorageConfig::create([
'field_name' => 'field_billing_email',
......
......@@ -4,6 +4,7 @@ namespace Drupal\Tests\violinist_teams\Kernel;
use Drupal\node\Entity\Node;
use Drupal\user\Entity\User;
use Drupal\violinist_teams\PlanInterface;
use Drupal\violinist_teams\TeamNode;
/**
......@@ -134,6 +135,36 @@ class TeamNodeTest extends KernelTestBase {
self::assertEquals('This is another test', $node->getPullRequestInstructions());
}
/**
* Test all the things with limits.
*/
public function testTeamLimits() {
/** @var \Drupal\violinist_teams\TeamNode $node */
$node = Node::create([
'type' => $this->nodeType->id(),
]);
// Default is to have one. When using the plan, that is.
self::assertEquals(1, $node->getPlan()->getMaxPrivateRepos());
// The actual limit will be 0 now though.
self::assertEquals(0, $node->getMaxPrivateRepos());
self::assertEquals(0, $node->getMaxPrivateReposWithoutOverride());
// Let's set a value for it.
$node->setMaxPrivateRepos(2);
self::assertEquals(2, $node->getMaxPrivateRepos());
self::assertEquals(2, $node->getMaxPrivateReposWithoutOverride());
// Still just the one on the plan, since they have not got a plan yet.
self::assertEquals(1, $node->getPlan()->getMaxPrivateRepos());
// Now let's set override.
$node->set('field_team_allowed_override', 3);
self::assertEquals(3, $node->getMaxPrivateRepos());
self::assertEquals(2, $node->getMaxPrivateReposWithoutOverride());
// Still just 1 on the plan.
self::assertEquals(1, $node->getPlan()->getMaxPrivateRepos());
// Now let's alter the plan, and make sure the override is used there.
$node->setPlan(PlanInterface::AGENCY_PLAN);
self::assertEquals(3, $node->getPlan()->getMaxPrivateRepos());
}
/**
* Dataprovider for environment variables.
*/
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment