activate example

$pluginBaseClass = $this->getPluginInstance($pluginBaseClassString);
        $activateContext = new ActivateContext(
            $pluginBaseClass,
            $shopwareContext,
            $this->shopwareVersion,
            $plugin->getVersion(),
            $this->createMigrationCollection($pluginBaseClass)
        );
        $activateContext->setAutoMigrate(false);

        $pluginBaseClass->activate($activateContext);

        $this->runMigrations($activateContext);

        if (!$shopwareContext->hasState(self::STATE_SKIP_ASSET_BUILDING)) {
            $this->assetInstaller->copyAssetsFromBundle($pluginBaseClassString);
        }

        $this->updatePluginData(
            [
                'id' => $plugin->getId(),
                'active' => true,
            ],

  }

  /** * {@inheritdoc} */
  protected function createEntity() {
    // Create a "Llama" user.     $user = User::create(['created' => 123456789]);
    $user->setUsername('Llama')
      ->setChangedTime(123456789)
      ->activate()
      ->save();

    return $user;
  }

  /** * {@inheritdoc} */
  protected function createAnotherEntity() {
    /** @var \Drupal\user\UserInterface $user */
    $user = $this->entity->createDuplicate();
    

  }

  /** * {@inheritdoc} */
  protected function createEntity() {
    // Create a "Llama" user.     $user = User::create(['created' => 123456789]);
    $user->setUsername('Llama')
      ->setChangedTime(123456789)
      ->activate()
      ->save();

    return $user;
  }

  /** * {@inheritdoc} */
  protected function createAnotherEntity($key) {
    /** @var \Drupal\user\UserInterface $user */
    $user = $this->getEntityDuplicate($this->entity, $key);
    


  /** * {@inheritdoc} */
  public function createNewEntity($entity_type_id$bundle$label$uid) {
    $user = parent::createNewEntity($entity_type_id$bundle$label$uid);

    // In order to create a referenceable user, it needs to be active.     if (!$this->currentUser->hasPermission('administer users')) {
      /** @var \Drupal\user\UserInterface $user */
      $user->activate();
    }

    return $user;
  }

  /** * {@inheritdoc} */
  public function validateReferenceableNewEntities(array $entities) {
    $entities = parent::validateReferenceableNewEntities($entities);
    // Mirror the conditions checked in buildEntityQuery().
// For non-admin users, populate the form fields using data from the     // browser.     if (!$admin) {
      $form['#attributes']['data-user-info-from-browser'] = TRUE;
    }

    // Because the user status has security implications, users are blocked by     // default when created programmatically and need to be actively activated     // if needed. When administrators create users from the user interface,     // however, we assume that they should be created as activated by default.     if ($admin) {
      $account->activate();
    }

    // Start with the default user account fields.     $form = parent::form($form$form_state);

    return $form;
  }

  /** * {@inheritdoc} */
  
/** @var AppCollection $apps */
        $apps = $this->appRepository->search(new Criteria()$this->context)->getEntities();

        static::assertCount(0, $apps);
    }

    public function testActivateExtension(): void
    {
        $this->installApp(__DIR__ . '/../_fixtures/TestApp');

        $this->lifecycleService->activate('app', 'TestApp', $this->context);

        /** @var AppCollection $apps */
        $apps = $this->appRepository->search(new Criteria()$this->context)->getEntities();

        static::assertCount(1, $apps);

        $testApp = $apps->first();

        static::assertNotNull($testApp);
        static::assertEquals('TestApp', $testApp->getName());
        static::assertTrue($testApp->isActive());
    }
    $author_name = 'This is a random & " > string';
    $comment->setOwnerId(0)->setAuthorName($author_name);
    $input = '[comment:author]';
    $output = $token_service->replace($input['comment' => $comment]['langcode' => $language_interface->getId()]);
    $this->assertSame((string) Html::escape($author_name)(string) $output);
    // Add comment field to user and term entities.     $this->addDefaultCommentField('user', 'user', 'comment', CommentItemInterface::OPEN, 'comment_user');
    $this->addDefaultCommentField('taxonomy_term', 'tags', 'comment', CommentItemInterface::OPEN, 'comment_term');

    // Create a user and a comment.     $user = User::create(['name' => 'alice']);
    $user->activate();
    $user->save();
    $this->postComment($user, 'user body', 'user subject', TRUE);

    // Create a term and a comment.     $term = Term::create([
      'vid' => 'tags',
      'name' => 'term',
    ]);
    $term->save();
    $this->postComment($term, 'term body', 'term subject', TRUE);

    
$this->assertHttpResponseWithMessage($response, 400, 'Missing credentials.pass.', $format);

    // Blocked.     $account
      ->block()
      ->save();

    $response = $this->loginRequest($name$pass$format);
    $this->assertHttpResponseWithMessage($response, 400, 'The user has not been activated or is blocked.', $format);

    $account
      ->activate()
      ->save();

    $response = $this->loginRequest($name, 'garbage', $format);
    $this->assertHttpResponseWithMessage($response, 400, 'Sorry, unrecognized username or password.', $format);

    $response = $this->loginRequest('garbage', $pass$format);
    $this->assertHttpResponseWithMessage($response, 400, 'Sorry, unrecognized username or password.', $format);

    $response = $this->loginRequest($name$pass$format);
    $this->assertEquals(200, $response->getStatusCode());
    $result_data = $this->serializer->decode($response->getBody()$format);
    

class UnblockUser extends ActionBase {

  /** * {@inheritdoc} */
  public function execute($account = NULL) {
    // Skip unblocking user if they are already unblocked.     if ($account !== FALSE && $account->isBlocked()) {
      $account->activate();
      $account->save();
    }
  }

  /** * {@inheritdoc} */
  public function access($object, AccountInterface $account = NULL, $return_as_object = FALSE) {
    /** @var \Drupal\user\UserInterface $object */
    $access = $object->status->access('edit', $account, TRUE)
      ->andIf($object->access('update', $account, TRUE));

    

    public function activatePlugin(Plugin $plugin)
    {
        $bootstrap = $this->getPluginByName($plugin->getName());
        $this->requirementValidator->validate($bootstrap->getPath() . '/plugin.xml', $this->release->getVersion());
        $context = new ActivateContext($plugin$this->release->getVersion()$plugin->getVersion());

        $this->events->notify(PluginEvent::PRE_ACTIVATE, new PrePluginActivateEvent($context$bootstrap));

        $bootstrap->activate($context);

        $this->events->notify(PluginEvent::POST_ACTIVATE, new PostPluginActivateEvent($context$bootstrap));

        $plugin->setActive(true);
        $plugin->setInSafeMode(false);
        $this->em->flush($plugin);

        return $context;
    }

    /** * @throws Exception * @throws \Doctrine\ORM\OptimisticLockException * * @return DeactivateContext */
if ($email_update_status_emails) {
        // Reset the configuration factory so it is updated with the new module.         $this->resetConfigFactory();
        $this->config('update.settings')->set('notification.emails', [$account_values['mail']])->save(TRUE);
      }
    }

    // We precreated user 1 with placeholder values. Let's save the real values.     $account = $this->userStorage->load(1);
    $account->init = $account->mail = $account_values['mail'];
    $account->roles = $account->getRoles();
    $account->activate();
    $account->timezone = $form_state->getValue('date_default_timezone');
    $account->pass = $account_values['pass'];
    $account->name = $account_values['name'];
    $account->save();
  }

}

  public function post(UserInterface $account = NULL) {
    $this->ensureAccountCanRegister($account);

    // Only activate new users if visitors are allowed to register and no email     // verification required.     if ($this->userSettings->get('register') == UserInterface::REGISTER_VISITORS && !$this->userSettings->get('verify_mail')) {
      $account->activate();
    }
    else {
      $account->block();
    }

    $this->checkEditFieldAccess($account);

    // Make sure that the user entity is valid (email and name are valid).     $this->validate($account);

    // Create the account.
'field_test_user' => [
        'entity' => $user,
      ],
    ]);

    $errors = $entity->validate();
    $this->assertCount(1, $errors);
    $this->assertEquals(new FormattableMarkup('This entity (%type: %label) cannot be referenced.', ['%type' => 'user', '%label' => $name])$errors[0]->getMessage());
    $this->assertEquals('field_test_user.0.entity', $errors[0]->getPropertyPath());

    // Activate the user and try again.     $user->activate();
    $errors = $entity->validate();
    $this->assertCount(0, $errors);

    // Test with a temporary and unsaved file.     $filename = $this->randomMachineName() . '.txt';
    $file = File::create([
      'filename' => $filename,
      'status' => 0,
    ]);

    $entity = EntityTest::create([
      
// Set up mocks so that writeAccessRestrictionFiles() can eventually use     // the IOInterface object.     $composer = $this->getMockBuilder(Composer::class)
      ->onlyMethods(['getPackage'])
      ->getMock();
    $composer->expects($this->once())
      ->method('getPackage')
      ->willReturn($this->prophesize(RootPackageInterface::class)->reveal());

    $plugin = new VendorHardeningPlugin();
    $plugin->activate($composer$this->prophesize(IOInterface::class)->reveal());

    $this->assertDirectoryExists($dir);

    $this->assertFileDoesNotExist($dir . '/.htaccess');
    $this->assertFileDoesNotExist($dir . '/web.config');

    $plugin->writeAccessRestrictionFiles($dir);

    $this->assertFileExists($dir . '/.htaccess');
    $this->assertFileExists($dir . '/web.config');
  }

  

  public function testAnonymousActivate() {
    $accountAnon = \Drupal::entityTypeManager()->getStorage('user')->load(0);

    // Test that the anonymous user is blocked.     $this->assertTrue($accountAnon->isBlocked());

    // Test that the anonymous user cannot be activated.     $this->expectException(\LogicException::class);
    $this->expectExceptionMessage('The anonymous user account should remain blocked at all times.');
    $accountAnon->activate();
  }

}
Home | Imprint | This part of the site doesn't use cookies.