Skip to content

Commit

Permalink
Merge branch '3.x' into issue/948
Browse files Browse the repository at this point in the history
  • Loading branch information
kedarkhaire authored Nov 28, 2023
2 parents f86e71d + 4833476 commit cf1cbd4
Show file tree
Hide file tree
Showing 8 changed files with 228 additions and 3 deletions.
1 change: 1 addition & 0 deletions modules/apigee_edge_teams/src/Entity/Team.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
* field_ui_base_route = "apigee_edge_teams.settings.team",
* )
*/
#[\AllowDynamicProperties]
class Team extends AttributesAwareFieldableEdgeEntityBase implements TeamInterface {

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
<?php

/**
* Copyright 2023 Google Inc.
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License version 2 as published by the
* Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
* License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc., 51
* Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

namespace Drupal\Tests\apigee_edge_teams\Functional\ApigeeX;

use Apigee\Edge\Structure\AttributesProperty;
use Drupal\Core\Session\UserSession;
use Drupal\user\Entity\User;

/**
* Team membership manager service test.
*
* @group apigee_edge
* @group apigee_edge_teams
*/
class TeamMembershipManagerTest extends ApigeeEdgeTeamsFunctionalTestBase {

/**
* The developer entity storage.
*
* @var \Drupal\apigee_edge\Entity\Storage\DeveloperStorageInterface
*/
protected $developerStorage;

/**
* The team entity storage.
*
* @var \Drupal\apigee_edge_teams\Entity\Storage\TeamStorageInterface
*/
protected $teamStorage;

/**
* Team entity to test.
*
* @var \Drupal\apigee_edge_teams\Entity\TeamInterface
*/
protected $team;

/**
* Array of developers to test.
*
* @var \Drupal\apigee_edge\Entity\DeveloperInterface[]
*/
protected $developers;

/**
* Drupal developer user account.
*
* @var \Drupal\Core\Session\AccountInterface
*/
protected $account;

/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();

$this->developerStorage = $this->container->get('entity_type.manager')->getStorage('developer');
$this->teamStorage = $this->container->get('entity_type.manager')->getStorage('team');

for ($i = 0; $i < 2; $i++) {
$name = strtolower($this->randomMachineName());

// Developer.
$account = $this->createAccount(array_keys(\Drupal::service('user.permissions')->getPermissions()));
$this->account = new UserSession([
'uid' => $account->id(),
'uuid' => $account->id(),
'name' => $account->getAccountName(),
'roles' => $account->getRoles(),
'mail' => $account->getEmail(),
]);
$this->developers[$i] = $account;
}

$this->team = $this->teamStorage->create([
'name' => $this->getRandomGenerator()->name(),
'attributes' => new AttributesProperty([
'name' => '__apigee_reserved__developer_details',
'value' => '[{\"developer\":\"[email protected]\",\"roles\":[\"admin\"]}]'
]),
]);
$this->team->save();
}

/**
* {@inheritdoc}
*/
protected function tearDown(): void {
foreach ($this->developers as $developer) {
try {
$this->developerStorage->delete([$developer]);
}
catch (\Exception $exception) {
$this->logException($exception);
}
}

if ($this->team !== NULL) {
try {
$this->teamStorage->delete([$this->team]);
}
catch (\Exception $exception) {
$this->logException($exception);
}
}

parent::tearDown();
}

/**
* Tests team membership manager service.
*/
public function testTeamMembershipManager() {

$team_membership_manager = $this->container->get('apigee_edge_teams.team_membership_manager');
$team_membership_cache = $this->container->get('apigee_edge_teams.cache.appgroup_membership_object');

// Ensure that the appgroup's member list is empty.
foreach ($this->developers as $developer) {
$this->assertEmpty($team_membership_manager->getTeams($developer->getEmail(), $this->team->getName()));
$this->assertEmpty($team_membership_manager->getMembers($this->team->getName()));
}
// Ensure that team membership cache is empty.
$this->assertEmpty($team_membership_cache->getMembership($this->team->getName())->getMembers());

// Adding the team members.
foreach ($this->developers as $developer) {
/** @var \Drupal\apigee_edge_teams\Entity\TeamMemberRoleInterface $team_member_roles */
$team_member_role_storage = \Drupal::entityTypeManager()->getStorage('team_member_role');
$team_member_role_storage->addTeamRoles($developer, $this->team, ['member']);
$team_member_roles = $team_member_role_storage->loadByDeveloperAndTeam($developer, $this->team);
$team_member_roles->save();
}

// Add developers to the team and check whether the related membership
// service functions work properly.
$team_membership_manager->addMembers($this->team->getName(), [$this->developers[0]->getEmail() => ['admin'], $this->developers[1]->getEmail() => ['member']]);

foreach ($this->developers as $developer) {
$this->assertContains($this->team->getName(), $team_membership_manager->getTeams($developer->getEmail(), $this->team->getName()));
$this->assertContains($developer->getEmail(), $team_membership_manager->getMembers($this->team->getName()));

// Check whether the team membership is correctly cached.
$this->assertArrayHasKey($developer->getEmail(), $team_membership_cache->getMembership($this->team->getName())->getMembers());
}

// Remove developers from the team and check whether the related
// membership service functions work properly.
foreach ($this->developers as $developer) {
$team_membership_manager->removeMembers($this->team->getName(), [$developer->getEmail()]);
$this->assertNotContains($this->team->getName(), $team_membership_manager->getTeams($developer->getEmail(), $this->team->getName()));
$this->assertNotContains($developer->getEmail(), $team_membership_manager->getMembers($this->team->getName()));

// Check whether the team membership is correctly cached.
$this->assertArrayNotHasKey($developer->getEmail(), $team_membership_cache->getMembership($this->team->getName())->getMembers());
}

// Ensure that team membership cache is empty.
$this->assertEmpty($team_membership_cache->getMembership($this->team->getName())->getMembers());

// Add developer to appgroup then delete developer and check whether the
// developer is no longer member of the team.
$team_membership_manager->addMembers($this->team->getName(), [$this->developers[0]->getEmail() => ['admin'], $this->developers[1]->getEmail() => ['member']]);
$developer1 = $this->developerStorage->load($this->developers[0]->getEmail());
$developer1->delete([$this->developers[0]]);
$this->assertNotContains($this->developers[0]->getEmail(), $team_membership_manager->getMembers($this->team->getName()));
// Check whether the team membership is correctly cached.
$this->assertArrayNotHasKey($this->developers[0]->getEmail(), $team_membership_cache->getMembership($this->team->getName())->getMembers());

// Delete the team and ensure that the team is removed from the team
// membership cache.
$this->teamStorage->delete([$this->team]);
}

}
7 changes: 7 additions & 0 deletions src/Command/CommandBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

namespace Drupal\apigee_edge\Command;

@trigger_error('The ' . __NAMESPACE__ . '\CommandBase is deprecated in apigee_edge:3.0.4 and is removed from apigee_edge:3.1.0. Drupal/console is not compatible with Drupal 10. See https://github.com/apigee/apigee-edge-drupal/issues/984', E_USER_DEPRECATED);

use Drupal\apigee_edge\CliServiceInterface;
use Drupal\Console\Core\Command\Command;
use Drupal\Console\Core\Command\Shared\CommandTrait;
Expand All @@ -32,6 +34,11 @@

/**
* Class CommandBase for shared functionality.
*
* @deprecated in apigee_edge:3.0.4 and is removed from apigee_edge:3.1.0.
* Drupal/console is not compatible with Drupal 10.
*
* @see https://github.com/apigee/apigee-edge-drupal/issues/984
*/
abstract class CommandBase extends Command {

Expand Down
6 changes: 6 additions & 0 deletions src/Command/CreateEdgeRoleCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@
* extension="apigee_edge",
* extensionType="module"
* )
*
* @deprecated in apigee_edge:3.0.4 and is removed from apigee_edge:3.1.0.
* Drupal/console is not compatible with Drupal 10.
*
* @see https://github.com/apigee/apigee-edge-drupal/issues/984
* @phpstan-ignore-next-line
*/
class CreateEdgeRoleCommand extends CommandBase {

Expand Down
6 changes: 6 additions & 0 deletions src/Command/DeveloperSyncCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@
* extension="apigee_edge",
* extensionType="module"
* )
*
* @deprecated in apigee_edge:3.0.4 and is removed from apigee_edge:3.1.0.
* Drupal/console is not compatible with Drupal 10.
*
* @see https://github.com/apigee/apigee-edge-drupal/issues/984
* @phpstan-ignore-next-line
*/
class DeveloperSyncCommand extends CommandBase {

Expand Down
5 changes: 5 additions & 0 deletions src/Command/DrupalConsoleLog.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@

/**
* Redirects Drupal logging messages to Drupal Console log.
*
* @deprecated in apigee_edge:3.0.4 and is removed from apigee_edge:3.1.0.
* Drupal/console is not compatible with Drupal 10.
*
* @see https://github.com/apigee/apigee-edge-drupal/issues/984
*/
class DrupalConsoleLog implements LoggerInterface {

Expand Down
11 changes: 8 additions & 3 deletions tests/src/Functional/DeveloperAppPermissionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use Apigee\Edge\Api\Management\Entity\App;
use Drupal\apigee_edge\Entity\Developer;
use Drupal\apigee_edge\Entity\DeveloperApp;
use Drupal\user\Entity\Role;
use Drupal\user\RoleInterface;

/**
Expand Down Expand Up @@ -203,9 +204,13 @@ public function testPermissions() {
*/
protected function revokeDefaultAuthUserPermissions() {
$definition = $this->entityType;
// @todo user_role_permissions() is deprecated for Drupal 10.1 https://www.drupal.org/node/3348138
// @phpstan-ignore-next-line
$user_permissions = user_role_permissions([RoleInterface::AUTHENTICATED_ID]);
$roles = [RoleInterface::AUTHENTICATED_ID];
$entities = Role::loadMultiple($roles);

$user_permissions = [];
foreach ($roles as $rid) {
$user_permissions[$rid] = $entities[$rid]->getPermissions() ?: [];
}
$authenticated_user_permissions = array_filter($user_permissions[RoleInterface::AUTHENTICATED_ID], function ($perm) use ($definition) {
return preg_match("/own {$definition->id()}$/", $perm);
});
Expand Down
2 changes: 2 additions & 0 deletions tests/src/Unit/Command/CreateEdgeRoleCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
* Test ApigeeEdgeCommands class.
*
* @group apigee_edge
*
* @group legacy
*/
class CreateEdgeRoleCommandTest extends UnitTestCase {

Expand Down

0 comments on commit cf1cbd4

Please sign in to comment.