You are here

api_test.install in Lightning API 8

Same filename and directory in other branches
  1. 8.2 modules/api_test/api_test.install

File

modules/api_test/api_test.install
View source
<?php

/**
 * Creates a OAuth2 Client with the Page Creator role scope and a related user.
 */
use Drupal\node\Entity\Node;
use Drupal\user\Entity\Role;
use Drupal\user\Entity\User;
use Drupal\consumers\Entity\Consumer;

/**
 * Implements hook_install().
 */
function api_test_install() {

  // Create a sample role to use for testing.
  $permissions = [
    "access content",
    "access contextual links",
    "access toolbar",
    "bypass node access",
    "create page content",
    "create url aliases",
    "delete page revisions",
    "edit any page content",
    "edit own page content",
    "revert page revisions",
    "use text format rich_text",
    "view all revisions",
    "view own unpublished content",
    "view page revisions",
  ];
  $role = Role::create([
    'id' => 'test-role-page_creator',
    'label' => 'Test Role Page Creator',
  ]);
  $role
    ->save();
  user_role_grant_permissions($role
    ->id(), $permissions);

  // Create a sample user to use with the client.
  $edit = [
    'name' => 'api-test-user',
    'mail' => 'api-test-user@example.com',
    'pass' => 'admin',
  ];
  $account = User::create($edit);
  $account
    ->addRole('test-role-page_creator');
  $account
    ->activate();
  $account
    ->save();
  $api_test_user_id = $account
    ->id();

  // Create an example client to use for testing.
  $data = [
    'uuid' => 'api_test-oauth2-client',
    'label' => 'API Test Client',
    'secret' => 'oursecret',
    'confidential' => 1,
    'user_id' => $api_test_user_id,
    'roles' => 'test-role-page_creator',
  ];
  $client = Consumer::create($data);
  $client
    ->save();

  // Create some sample content for testing. One published and one unpublished
  // basic page.
  $data = [
    'title' => 'Unpublished Page',
    'type' => 'page',
    'uuid' => 'api_test-unpublished-page-content',
    'body' => '--TESTING--',
    'status' => FALSE,
  ];
  $node = Node::create($data);
  $node
    ->save();
  $data = [
    'title' => 'Published Page',
    'type' => 'page',
    'uuid' => 'api_test-published-page-content',
    'body' => '--TESTING--',
    'status' => TRUE,
  ];
  $node = Node::create($data);
  $node
    ->save();
}

/**
 * Implements hook_uninstall().
 */
function api_test_uninstall() {

  // Delete the user created during install.
  $username = 'api-test-user';
  $storage = \Drupal::entityTypeManager()
    ->getStorage('user');
  $users = $storage
    ->loadByProperties([
    'name' => $username,
  ]);
  $storage
    ->delete($users);

  // Delete the client created during install.
  $client_uuid = 'api_test-oauth2-client';
  $storage = \Drupal::entityTypeManager()
    ->getStorage('consumer');
  $clients = $storage
    ->loadByProperties([
    'uuid' => $client_uuid,
  ]);
  $storage
    ->delete($clients);

  // Delete the nodes created during install.
  $storage = \Drupal::entityTypeManager()
    ->getStorage('node');
  $nodes = $storage
    ->loadByProperties([
    'body' => '--TESTING--',
  ]);
  $storage
    ->delete($nodes);
}

Functions

Namesort descending Description
api_test_install Implements hook_install().
api_test_uninstall Implements hook_uninstall().