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

tests/src/Functional/OgSelectionWidgetAutoCompleteTest.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\og\OgRoleManagerInterface;
use Drupal\Tests\BrowserTestBase;
use Drupal\user\Entity\Role;
use Drupal\user\UserInterface;

/**
 * Tests Og auto complete widget.
 *
 * @group og
 */
class OgSelectionWidgetAutoCompleteTest extends BrowserTestBase {

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

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

  /**
   * A group node for user 1.
   */
  protected NodeInterface $group1;

  /**
   * A group node for user 2.
   */
  protected NodeInterface $group2;

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

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

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

  /**
   * OG role manager service.
   */
  protected OgRoleManagerInterface $roleManager;

  /**
   * The role of the user.
   */
  protected OgRoleInterface $role;

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

    // Create group node types.
    $this->createContentType(['name' => 'group_type', 'type' => 'group_type']);
    Og::addGroup('node', 'group_type');

    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' => 'entity_reference_autocomplete',
      ],
    ];
    Og::createField(OgGroupAudienceHelperInterface::DEFAULT_FIELD, 'node', 'group_content', $settings);

    // Create users.
    $this->user1 = $this->drupalCreateUser();
    $this->user2 = $this->drupalCreateUser();

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

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

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

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

    // Adding to the member role the appropriate permission.
    $this->role = OgRole::create();
    $this->role
      ->setName('content_editor')
      ->setLabel('Content group editor')
      ->setGroupType('node')
      ->setGroupBundle('group_type')
      ->grantPermission('create group_content content')
      ->save();
  }

  /**
   * Test the auto complete widget for non group member.
   */
  public function testAutoCompleteForNonGroupMember(): void {
    $this->drupalLogin($this->user1);

    // Verify that users can't reference groups they don't belong to.
    $edit = [
      'title[0][value]' => $this->randomMachineName(),
      'og_audience[0][target_id]' => $this->group2->label() . ' (' . $this->group2->id() . ')',
    ];

    $this->drupalGet('node/add/group_content');
    $this->submitForm($edit, 'Save');

    // Test ValidOgMembershipReference constraint validates the field correctly.
    $this->assertSession()->pageTextContains('You are not allowed to post content in this group.');

    // Add the member to the group.
    Og::createMembership($this->group2, $this->user1)->addRole($this->role)->save();

    $this->drupalLogin($this->user1);
    // Testing the user can add group content after being a member of the group.
    $edit = [
      'title[0][value]' => $this->randomMachineName(),
      'og_audience[0][target_id]' => $this->group2->label() . ' (' . $this->group2->id() . ')',
    ];

    $this->drupalGet('node/add/group_content');
    $this->submitForm($edit, 'Save');
    $this->assertSession()->pageTextContains($edit['title[0][value]'] . ' has been created.');
  }

  /**
   * Tests that a group member can reference a valid group entity.
   *
   * Ensures no error messages appear when correctly assigning the entity.
   */
  public function testGroupMemberCanReferenceGroup(): void {
    // Give the group member user the correct permission directly.
    $this->groupMemberUser = $this->drupalCreateUser(['edit any group_content content']);
    Og::createMembership($this->group1, $this->groupMemberUser)->save();

    // Give 'group_type' members the correct permission using OgRole.
    $role = OgRole::loadByGroupAndName($this->group1, OgRoleInterface::AUTHENTICATED);
    $role->grantPermission('create group_content content');
    $role->grantPermission('edit any group_content content');
    $role->save();

    // Log in as the group member.
    $this->drupalLogin($this->groupMemberUser);

    // Create new group content.
    $new_group_content = Node::create([
      'type' => 'group_content',
      'title' => 'Test Node',
      'body' => [
        'value' => 'This is a test group content.',
      ],
      'uid' => $this->groupMemberUser->id(),
      'status' => 1,
    ]);
    $new_group_content->save();

    // Go to edit form and set og_audience.
    $nid = $new_group_content->id();
    $this->drupalGet("node/{$nid}/edit");
    $edit = [
      'og_audience[0][target_id]' => $this->group1->label() . ' (' . $this->group1->id() . ')',
    ];
    $this->submitForm($edit, 'Save');

    // Check for confirmation.
    $this->assertSession()->statusMessageContains('has been updated.', 'status');
  }

}

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

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