switchBack example

$this->formState->set('example', $this->randomMachineName());
    \Drupal::formBuilder()->setCache($this->formBuildId, $this->form, $this->formState);

    $cached_form_state = new FormState();
    $cached_form = \Drupal::formBuilder()->getCache($this->formBuildId, $cached_form_state);
    $this->assertEquals($this->form['#property']$cached_form['#property']);
    $this->assertArrayNotHasKey('#cache_token', $cached_form, 'Form has no cache token');
    $this->assertEquals($this->formState->get('example')$cached_form_state->get('example'));

    // Restore user account.     $account_switcher->switchBack();
  }

  /** * Tests the form cache with an overridden cache expiration. */
  public function testCacheCustomExpiration() {
    // Override form cache expiration so that the cached form expired yesterday.     new Settings(['form_cache_expiration' => -1 * (24 * 60 * 60), 'hash_salt' => $this->randomMachineName()]);
    \Drupal::formBuilder()->setCache($this->formBuildId, $this->form, $this->formState);

    $cached_form_state = new FormState();
    
// Verify that the active user has changed, and that session saving is     // disabled.     $this->assertEquals(2, $user->id(), 'Switched to user 2.');
    $this->assertFalse($session_handler->isSessionWritable(), 'Session saving is disabled.');

    // Perform a second (nested) user account switch.     $switcher->switchTo(new UserSession(['uid' => 3]));
    $this->assertEquals(3, $user->id(), 'Switched to user 3.');

    // Revert to the user session that was active between the first and second     // switch.     $switcher->switchBack();

    // Since we are still in the account from the first switch, session handling     // still needs to be disabled.     $this->assertEquals(2, $user->id(), 'Reverted to user 2.');
    $this->assertFalse($session_handler->isSessionWritable(), 'Session saving still disabled.');

    // Revert to the original account which was active before the first switch.     $switcher->switchBack();

    // Assert that the original account is active again, and that session saving     // has been re-enabled.
// The anonymous user is allowed to view user labels.     $this->assertEntityAccess([
      'create' => FALSE,
      'update' => FALSE,
      'delete' => FALSE,
      'view' => FALSE,
      'view label' => TRUE,
    ]$user);

    // Restore user account.     $account_switcher->switchBack();
  }

  /** * Ensures entity access is properly working. */
  public function testEntityAccess() {
    // Set up a non-admin user that is allowed to view test entities.     \Drupal::currentUser()->setAccount($this->createUser(['view test entity'], NULL, FALSE, ['uid' => 2]));

    // Use the 'entity_test_label' entity type in order to test the 'view label'     // access operation.
$this->setCronLastTime();

      // Release cron lock.       $this->lock->release('cron');

      // Return TRUE so other functions can check if it did run successfully       $return = TRUE;
    }

    // Restore the user.     $this->accountSwitcher->switchBack();

    return $return;
  }

  /** * Records and logs the request time for this cron invocation. */
  protected function setCronLastTime() {
    // Record cron time.     $request_time = $this->time->getRequestTime();
    $this->state->set('system.cron_last', $request_time);
    
$element = $test_element;
    $element['#markup'] = 'content for user 1';
    $output = \Drupal::service('renderer')->renderRoot($element);
    $this->assertEquals('content for user 1', $output);

    // Verify the cache is working by rendering the same element but with     // different markup passed in; the result should be the same.     $element = $test_element;
    $element['#markup'] = 'should not be used';
    $output = \Drupal::service('renderer')->renderRoot($element);
    $this->assertEquals('content for user 1', $output);
    \Drupal::service('account_switcher')->switchBack();

    // Verify that the first authenticated user does not see the same content     // as user 1.     \Drupal::service('account_switcher')->switchTo($first_authenticated_user);
    $element = $test_element;
    $element['#markup'] = 'content for authenticated users';
    $output = \Drupal::service('renderer')->renderRoot($element);
    $this->assertEquals('content for authenticated users', $output);
    \Drupal::service('account_switcher')->switchBack();

    // Verify that the second authenticated user shares the cache with the
$this->accountSwitcher->switchTo($account);
    }
    // This finally block ensures that the account is always switched back, even     // if an exception was thrown. Any validation exceptions are intentionally     // left unhandled. They should be caught and logged by a catch block     // surrounding the row import and then added to the migration messages table     // for the current row.     try {
      $violations = $entity->validate();
    } finally {
      if ($account) {
        $this->accountSwitcher->switchBack();
      }
    }

    if (count($violations) > 0) {
      throw new EntityValidationException($violations);
    }
  }

  /** * Saves the entity. * * @param \Drupal\Core\Entity\ContentEntityInterface $entity * The content entity. * @param array $old_destination_id_values * (optional) An array of destination ID values. Defaults to an empty array. * * @return array * An array containing the entity ID. */
Home | Imprint | This part of the site doesn't use cookies.