You are here

public function PathautoMassDeleteTest::generateAliases in Pathauto 8

Helper function to generate aliases.

1 call to PathautoMassDeleteTest::generateAliases()
PathautoMassDeleteTest::testDeleteAll in tests/src/Functional/PathautoMassDeleteTest.php
Tests the deletion of all the aliases.

File

tests/src/Functional/PathautoMassDeleteTest.php, line 146

Class

PathautoMassDeleteTest
Mass delete functionality tests.

Namespace

Drupal\Tests\pathauto\Functional

Code

public function generateAliases() {

  // Delete all aliases to avoid duplicated aliases. They will be recreated
  // below.
  $this
    ->deleteAllAliases();

  // We generate a bunch of aliases for nodes, users and taxonomy terms. If
  // the entities are already created we just update them, otherwise we create
  // them.
  if (empty($this->nodes)) {

    // Create a large number of nodes (100+) to make sure that the batch code
    // works.
    for ($i = 1; $i <= 105; $i++) {

      // Set the alias of two nodes manually.
      $settings = $i > 103 ? [
        'path' => [
          'alias' => "/custom_alias_{$i}",
          'pathauto' => PathautoState::SKIP,
        ],
      ] : [];
      $node = $this
        ->drupalCreateNode($settings);
      $this->nodes[$node
        ->id()] = $node;
    }
  }
  else {
    foreach ($this->nodes as $node) {
      if ($node
        ->id() > 103) {

        // The alias is set manually.
        $node
          ->set('path', [
          'alias' => '/custom_alias_' . $node
            ->id(),
        ]);
      }
      $node
        ->save();
    }
  }

  // Delete information about the state of an alias to make sure that aliases
  // with no such data are left alone by default.
  \Drupal::keyValue('pathauto_state.node')
    ->delete(101);
  if (empty($this->accounts)) {
    for ($i = 1; $i <= 5; $i++) {
      $account = $this
        ->drupalCreateUser();
      $this->accounts[$account
        ->id()] = $account;
    }
  }
  else {
    foreach ($this->accounts as $account) {
      $account
        ->save();
    }
  }
  if (empty($this->terms)) {
    $vocabulary = $this
      ->addVocabulary([
      'name' => 'test vocabulary',
      'vid' => 'test_vocabulary',
    ]);
    for ($i = 1; $i <= 5; $i++) {
      $term = $this
        ->addTerm($vocabulary);
      $this->terms[$term
        ->id()] = $term;
    }
  }
  else {
    foreach ($this->terms as $term) {
      $term
        ->save();
    }
  }

  // Check that we have aliases for the entities.
  foreach ([
    'nodes',
    'accounts',
    'terms',
  ] as $attribute) {
    foreach ($this->{$attribute} as $entity) {
      $this
        ->assertEntityAliasExists($entity);
    }
  }
}