og-8.x-1.x-dev/tests/src/Functional/OgSelectionWidgetOptionsTest.php

tests/src/Functional/OgSelectionWidgetOptionsTest.php
<?php

declare(strict_types=1);

namespace Drupal\Tests\og\Functional;

use Drupal\node\Entity\Node;
use Drupal\node\Entity\NodeType;
use Drupal\node\NodeInterface;
use Drupal\og\Entity\OgRole;
use Drupal\og\Og;
use Drupal\og\OgGroupAudienceHelperInterface;
use Drupal\og\OgRoleInterface;
use Drupal\Tests\BrowserTestBase;
use Drupal\user\Entity\Role;
use Drupal\user\UserInterface;

/**
 * Tests the various OG handler options.
 *
 * @group og
 */
class OgSelectionWidgetOptionsTest extends BrowserTestBase {

  /**
   * {@inheritdoc}
   */
  protected static $modules = [
    'node',
    'og',
    'og_ui',
  ];

  /**
   * {@inheritdoc}
   */
  protected $defaultTheme = 'stark';

  /**
   * A group node.
   */
  protected NodeInterface $group1;

  /**
   * A group node.
   */
  protected NodeInterface $group2;

  /**
   * An unpublished group node.
   */
  protected NodeInterface $unpublishedGroup;

  /**
   * Demo user.
   */
  protected UserInterface $groupMemberUser;

  /**
   * Group owner.
   */
  protected UserInterface $groupOwnerUser;

  /**
   * Administrator groups user.
   */
  protected UserInterface $groupAdministratorUser;

  /**
   * A non-member user.
   */
  protected UserInterface $nonMemberUser;

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

    // Create group node types.
    $this->createContentType([
      'name' => 'group type 1',
      'type' => 'group_type1',
    ]);
    $this->createContentType([
      'name' => 'group type 2',
      'type' => 'group_type2',
    ]);

    Og::addGroup('node', 'group_type1');
    Og::addGroup('node', 'group_type2');

    NodeType::create(['type' => 'group_content', 'name' => 'group_content'])->save();

    // Use a select list widget for the audience field, so it's easier to get
    // all the values.
    $settings = [
      'form_display' => [
        'type' => 'options_select',
      ],
    ];
    Og::createField(OgGroupAudienceHelperInterface::DEFAULT_FIELD, 'node', 'group_content', $settings);

    // Create users.
    $this->groupMemberUser = $this->drupalCreateUser([], 'group member');
    $this->groupOwnerUser = $this->drupalCreateUser([], 'group owner');
    $this->groupAdministratorUser = $this->drupalCreateUser(['administer organic groups']);
    $this->nonMemberUser = $this->drupalCreateUser();

    // Create groups.
    $this->group1 = Node::create([
      'type' => 'group_type1',
      'title' => 'group1',
      'uid' => $this->groupOwnerUser->id(),
    ]);
    $this->group1->save();

    $this->group2 = Node::create([
      'type' => 'group_type2',
      'title' => 'group2',
      'uid' => $this->groupOwnerUser->id(),
    ]);
    $this->group2->save();

    $this->unpublishedGroup = Node::create([
      'type' => 'group_type2',
      'title' => 'unpublished group',
      'uid' => $this->groupOwnerUser->id(),
      'status' => NodeInterface::NOT_PUBLISHED,
    ]);
    $this->unpublishedGroup->save();

    // Add member to group.
    Og::createMembership($this->group1, $this->groupMemberUser)->save();
  }

  /**
   * Tests the group audience widgets shows correct values.
   */
  public function testNonRequiredAudienceField(): void {
    // Non member user.
    $this->drupalLogin($this->nonMemberUser);
    $this->drupalGet('node/add/group_content');
    $this->assertSession()->statusCodeEquals(403);

    // Group member without create permissions.
    $this->drupalLogin($this->groupMemberUser);
    $this->drupalGet('node/add/group_content');
    $this->assertSession()->statusCodeEquals(403);

    // Grant create permission for the first group.
    $role = OgRole::getRole($this->group1->getEntityTypeId(), $this->group1->bundle(), OgRoleInterface::AUTHENTICATED);
    $role
      ->grantPermission('create group_content content')
      ->save();

    // Give authenticated users permission to create the group content.
    $role = Role::load('authenticated');

    $role->grantPermission('create group_content content');
    $role->save();

    $this->drupalGet('node/add/group_content');
    // The user can't post group content site wide so the audience is required
    // field.
    $this->assertSession()->optionExists('Groups audience', '_none');
    $this->assertSession()->optionExists('Groups audience', $this->group1->label());
    $this->assertSession()->optionNotExists('Groups audience', $this->group2->label());

    // Group owner.
    $this->drupalLogin($this->groupOwnerUser);
    $this->drupalGet('node/add/group_content');

    $this->assertSession()->optionExists('Groups audience', $this->group1->label());
    $this->assertSession()->optionExists('Groups audience', $this->group2->label());
    $this->assertSession()->optionNotExists('Groups audience', $this->unpublishedGroup->label());

    // Site-wide administrator.
    $this->drupalLogin($this->groupAdministratorUser);
    $this->drupalGet('node/add/group_content');
    $this->assertSession()->optionExists('Groups audience', $this->group1->label());
    $this->assertSession()->optionExists('Groups audience', $this->group2->label());
    $this->assertSession()->optionNotExists('Groups audience', $this->unpublishedGroup->label());
  }

}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc