og-8.x-1.x-dev/tests/src/Kernel/Views/OgMembershipViewTest.php

tests/src/Kernel/Views/OgMembershipViewTest.php
<?php

declare(strict_types=1);

namespace Drupal\Tests\og\Kernel\Views;

use Drupal\node\NodeInterface;
use Drupal\Tests\user\Traits\UserCreationTrait;
use Drupal\Tests\views\Kernel\ViewsKernelTestBase;
use Drupal\node\Entity\Node;
use Drupal\node\Entity\NodeType;
use Drupal\og\Og;
use Drupal\user\UserInterface;
use Drupal\views\Tests\ViewResultAssertionTrait;
use Drupal\views\Tests\ViewTestData;
use Drupal\views\Views;

/**
 * Tests the OG Membership based View.
 *
 * @group og
 */
class OgMembershipViewTest extends ViewsKernelTestBase {

  use UserCreationTrait;
  use ViewResultAssertionTrait;

  /**
   * {@inheritdoc}
   */
  protected static $modules = [
    'system',
    'user',
    'field',
    'node',
    'views',
    'og_test',
    'og',
    'options',
  ];

  /**
   * Views used by this test.
   *
   * @var list<string>
   *
   * @phpcsSuppress SlevomatCodingStandard.TypeHints.PropertyTypeHint
   */
  public static $testViews = [
    'test_og_membership',
    'test_og_group_to_og_membership',
  ];

  /**
   * A column mapping common to the relationships tested.
   *
   * @var array<string, string>
   */
  protected array $columnMap = [
    'users_field_data_og_membership_uid' => 'uid',
    'node_field_data_og_membership_nid' => 'nid',
  ];

  /**
   * The group entity.
   */
  protected NodeInterface $group;

  /**
   * The group admin.
   */
  protected UserInterface $groupAdmin;

  /**
   * The group member.
   */
  protected UserInterface $groupMember;

  /**
   * {@inheritdoc}
   */
  protected function setUp($import_test_views = FALSE): void {
    parent::setUp($import_test_views);

    ViewTestData::createTestViews(static::class, ['og_test']);
  }

  /**
   * {@inheritdoc}
   */
  protected function setUpFixtures() {
    // Add membership and config schema.
    $this->installConfig(['og']);
    // Install the test view provided by the og_test module.
    $this->installConfig(['og_test']);
    $this->installEntitySchema('og_membership');
    $this->installEntitySchema('user');
    $this->installEntitySchema('node');

    // Create a group entity type.
    $group_bundle = mb_strtolower($this->randomMachineName());
    NodeType::create([
      'type' => $group_bundle,
      'name' => $this->randomString(),
    ])->save();
    Og::groupTypeManager()->addGroup('node', $group_bundle);

    // Create group admin user.
    $this->groupAdmin = $this->createUser(['access user profiles'], FALSE, TRUE);
    // Create group member user.
    $this->groupMember = $this->createUser();

    // Create a group.
    $this->group = Node::create([
      'title' => $this->randomString(),
      'type' => $group_bundle,
      'uid' => $this->groupAdmin->id(),
    ]);
    $this->group->save();

    // Create a membership linking the user to the group.
    $membership = Og::createMembership($this->group, $this->groupMember);
    $membership->save();

    parent::setUpFixtures();
  }

  /**
   * Tests the og_membership view.
   */
  public function testOgMembershipView(): void {
    $view = Views::getView('test_og_membership');
    $this->executeView($view);

    // Assert the result set lists the group owner, group, and member.
    $expected_result = [
      [
        'nid' => $this->group->id(),
        'uid' => $this->groupAdmin->id(),
      ],
      [
        'nid' => $this->group->id(),
        'uid' => $this->groupMember->id(),
      ],
    ];

    $this->assertIdenticalResultset($view, $expected_result, $this->columnMap);
  }

  /**
   * Tests the OG group to user through og_membership relationship.
   */
  public function testOgGroupToMemberView(): void {
    // Add a second group with only the group admin as member.
    $bundle = $this->group->bundle();
    $second_group = Node::create([
      'title' => $this->randomString(),
      'type' => $bundle,
      'uid'  => $this->groupAdmin->id(),
    ]);
    $second_group->save();

    $view = Views::getView('test_og_group_to_og_membership');
    $this->executeView($view);

    $expected_result = [
      [
        'nid' => $this->group->id(),
        'uid' => $this->groupAdmin->id(),
      ],
      [
        'nid' => $this->group->id(),
        'uid' => $this->groupMember->id(),
      ],
      [
        'nid' => $second_group->id(),
        'uid' => $this->groupAdmin->id(),
      ],
    ];

    $this->assertIdenticalResultset($view, $expected_result, $this->columnMap);
  }

}

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

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