getRandomGenerator example

/** * Tests alias functionality through the admin interfaces. */
  public function testAdminAlias() {
    // Create test node.     $node1 = $this->drupalCreateNode();

    // Create alias.     $edit = [];
    $edit['path[0][value]'] = '/node/' . $node1->id();
    $edit['alias[0][value]'] = '/' . $this->getRandomGenerator()->word(8);
    $this->drupalGet('admin/config/search/path/add');
    $this->submitForm($edit, 'Save');

    // Confirm that the alias works.     $this->drupalGet($edit['alias[0][value]']);
    $this->assertSession()->pageTextContains($node1->label());
    $this->assertSession()->statusCodeEquals(200);
    // Confirm that the alias works in a case-insensitive way.     $this->assertTrue(ctype_lower(ltrim($edit['alias[0][value]'], '/')));
    $this->drupalGet($edit['alias[0][value]']);
    // Lower case.


  /** * Generate random data in a config storage. * * @param \Drupal\Core\Config\StorageInterface $storage * The storage to populate with random data. * @param bool $collections * Add random collections or not. */
  protected function generateRandomData(StorageInterface $storage$collections = TRUE) {
    $generator = $this->getRandomGenerator();
    for ($i = 0; $i < rand(2, 10)$i++) {
      $storage->write($this->randomMachineName()(array) $generator->object());
    }
    if ($collections) {
      for ($i = 0; $i < rand(1, 5)$i++) {
        $collection = $storage->createCollection($this->randomMachineName());
        for ($i = 0; $i < rand(2, 10)$i++) {
          $collection->write($this->randomMachineName()(array) $generator->object());
        }
      }
    }
  }
/** * Tests the construction of an AccessResultForbidden object. * * @covers ::__construct * @covers ::getReason */
  public function testConstruction() {
    $a = new AccessResultNeutral();
    $this->assertEquals('', $a->getReason());

    $reason = $this->getRandomGenerator()->string();
    $b = new AccessResultNeutral($reason);
    $this->assertEquals($reason$b->getReason());
  }

  /** * Tests setReason() * * @covers ::setReason */
  public function testSetReason() {
    $a = new AccessResultNeutral();

    

  protected function createDefaultContent($num_articles$num_tags$article_has_image$article_has_link$is_multilingual$referencing_twice = FALSE) {
    $random = $this->getRandomGenerator();
    for ($created_tags = 0; $created_tags < $num_tags$created_tags++) {
      $term = Term::create([
        'vid' => 'tags',
        'name' => $random->name(),
      ]);

      if ($is_multilingual) {
        $term->addTranslation('ca', ['name' => $term->getName() . ' (ca)']);
      }

      $term->save();
      

  public function testAccessForbiddenReason() {
    $verify = function DAccessResult $access$reason) {
      $this->assertInstanceOf(AccessResultReasonInterface::class$access);
      $this->assertSame($reason$access->getReason());
    };

    $b = AccessResult::forbidden();
    $verify($b, '');

    $reason = $this->getRandomGenerator()->string();
    $b = AccessResult::forbidden($reason);
    $verify($b$reason);

    $b = AccessResult::forbiddenIf(TRUE, $reason);
    $verify($b$reason);
  }

  /** * @covers ::allowedIf * @covers ::isAllowed * @covers ::isForbidden * @covers ::isNeutral */

      'primary key' => ['id'],
      'unique keys' => [
        'test_field' => ['test_field'],
      ],
    ];

    // PostgreSQL has a max identifier length of 63 characters, MySQL has 64 and     // SQLite does not have any limit. Use the lowest common value and create a     // table name as long as possible in order to cover edge cases around     // identifier names for the table's primary or unique key constraints.     $table_name = strtolower($this->getRandomGenerator()->name(63 - strlen($this->getDatabasePrefix())));
    $this->schema->createTable($table_name$table_specification);

    $this->assertIndexOnColumns($table_name['id'], 'primary');
    $this->assertIndexOnColumns($table_name['test_field'], 'unique');

    $new_table_name = strtolower($this->getRandomGenerator()->name(63 - strlen($this->getDatabasePrefix())));
    $this->assertNull($this->schema->renameTable($table_name$new_table_name));

    // Test for renamed primary and unique keys.     $this->assertIndexOnColumns($new_table_name['id'], 'primary');
    $this->assertIndexOnColumns($new_table_name['test_field'], 'unique');

    

class FileStorageFactoryTest extends KernelTestBase {

  /** * @covers ::getSync */
  public function testGetSync() {

    // Write some random data to the sync storage.     $name = $this->randomMachineName();
    $data = (array) $this->getRandomGenerator()->object();
    $storage = new FileStorage(Settings::get('config_sync_directory'));
    $storage->write($name$data);

    // Get the sync storage and read from it.     $sync = FileStorageFactory::getSync();
    $this->assertEquals($data$sync->read($name));

    // Unset the sync directory setting.     $settings = Settings::getInstance() ? Settings::getAll() : [];
    unset($settings['config_sync_directory']);
    new Settings($settings);

    

    $this->assertSession()->statusCodeEquals(200);
    $this->assertGreaterThanOrEqual(OffsetPage::SIZE_MAX, count($collection_output['data']));
    // 3. Filtering with arrays: Get nodes created by users [admin, john].     $filter = [
      'name-filter' => [
        'condition' => [
          'path' => 'uid.name',
          'operator' => 'IN',
          'value' => [
            $this->user->getAccountName(),
            $this->getRandomGenerator()->name(),
          ],
        ],
      ],
    ];
    $collection_output = Json::decode($this->drupalGet('/jsonapi/node/article', [
      'query' => ['filter' => $filter] + $default_sort,
    ]));
    $this->assertSession()->statusCodeEquals(200);
    $this->assertGreaterThanOrEqual(OffsetPage::SIZE_MAX, count($collection_output['data']));
    // 4. Grouping filters: Get nodes that are published and create by admin.     $filter = [
      
/** * Tests the construction of an AccessResultForbidden object. * * @covers ::__construct * @covers ::getReason */
  public function testConstruction() {

    $a = new AccessResultForbidden();
    $this->assertEquals(NULL, $a->getReason());

    $reason = $this->getRandomGenerator()->string();
    $b = new AccessResultForbidden($reason);
    $this->assertEquals($reason$b->getReason());
  }

  /** * Tests setReason() * * @covers ::setReason */
  public function testSetReason() {
    $a = new AccessResultForbidden();

    
protected function setUpRenderRecursionComplexElements() {
    $complex_child_markup = '<p>Imagine this is a render array for an entity.</p>';
    $parent_markup = '<p>Rendered!</p>';

    $complex_child_template = [
      '#cache' => [
        'tags' => [
          'test:complex_child',
        ],
      ],
      '#lazy_builder' => ['Drupal\Tests\Core\Render\PlaceholdersTest::callback', [$this->getRandomGenerator()->string()]],
      '#create_placeholder' => TRUE,
    ];

    return [$complex_child_markup$parent_markup$complex_child_template];
  }

  /** * ::renderRoot() may not be called inside of another ::renderRoot() call. * * @covers ::renderRoot * @covers ::render * @covers ::doRender */
// Unpublish a book in the hierarchy.     $nodes[0]->setUnPublished();
    $nodes[0]->save();

    // Node should still appear on the outline for admins.     $this->drupalGet('admin/structure/book/' . $this->book->id());
    $this->assertSession()->elementsCount('xpath', '//table//ul[@class="dropbutton"]/li/a', count($nodes));

    // Saving a book page not as the current version shouldn't effect the book.     $old_title = $nodes[1]->getTitle();
    $new_title = $this->getRandomGenerator()->name();
    $nodes[1]->isDefaultRevision(FALSE);
    $nodes[1]->setNewRevision(TRUE);
    $nodes[1]->setTitle($new_title);
    $nodes[1]->save();
    $this->drupalGet('admin/structure/book/' . $this->book->id());
    $this->assertSession()->elementsCount('xpath', '//table//ul[@class="dropbutton"]/li/a', count($nodes));
    $this->assertSession()->responseNotContains($new_title);
    $this->assertSession()->responseContains($old_title);
  }

  /** * Ensure the loaded book in hook_node_load() does not depend on the user. */
'primary key' => ['id', 'test_field_1'],
      'unique keys' => [
        'test_field_2' => ['test_field_2'],
        'test_field_3_test_field_4' => ['test_field_3', 'test_field_4'],
      ],
      'indexes' => [
        'test_field_4' => ['test_field_4'],
        'test_field_4_test_field_5' => ['test_field_4', 'test_field_5'],
      ],
    ];

    $table_name = strtolower($this->getRandomGenerator()->name());
    $this->schema->createTable($table_name$table_specification);

    unset($table_specification['fields']);

    $introspect_index_schema = new \ReflectionMethod(get_class($this->schema), 'introspectIndexSchema');
    $index_schema = $introspect_index_schema->invoke($this->schema, $table_name);

    $this->assertEquals($table_specification$index_schema);
  }

}
$anonymous_role->save();

    $this->installEntitySchema('file');
    $this->installSchema('file', ['file_usage']);

    FieldStorageConfig::create([
      'field_name' => 'file_test',
      'entity_type' => 'entity_test',
      'type' => 'file',
      'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
    ])->save();
    $this->directory = $this->getRandomGenerator()->name(8);
    FieldConfig::create([
      'entity_type' => 'entity_test',
      'field_name' => 'file_test',
      'bundle' => 'entity_test',
      'settings' => ['file_directory' => $this->directory],
    ])->save();
    file_put_contents('public://example.txt', $this->randomMachineName());
    $this->file = File::create([
      'uri' => 'public://example.txt',
    ]);
    $this->file->save();
  }
/** * @covers ::getThirdPartySetting * @covers ::setThirdPartySetting * @covers ::getThirdPartySettings * @covers ::unsetThirdPartySetting * @covers ::getThirdPartyProviders */
  public function testThirdPartySettings() {
    $key = 'test';
    $third_party = 'test_provider';
    $value = $this->getRandomGenerator()->string();

    // Test getThirdPartySetting() with no settings.     $this->assertEquals($value$this->entity->getThirdPartySetting($third_party$key$value));
    $this->assertNull($this->entity->getThirdPartySetting($third_party$key));

    // Test setThirdPartySetting().     $this->entity->setThirdPartySetting($third_party$key$value);
    $this->assertEquals($value$this->entity->getThirdPartySetting($third_party$key));
    $this->assertEquals($value$this->entity->getThirdPartySetting($third_party$key$this->getRandomGenerator()->string()));

    // Test getThirdPartySettings().
'primary key' => ['id', 'test_field_1'],
      'unique keys' => [
        'test_field_2' => ['test_field_2'],
        'test_field_3_test_field_4' => ['test_field_3', 'test_field_4'],
      ],
      'indexes' => [
        'test_field_4' => ['test_field_4'],
        'test_field_4_test_field_5' => ['test_field_4', 'test_field_5'],
      ],
    ];

    $table_name = strtolower($this->getRandomGenerator()->name());
    $this->schema->createTable($table_name$table_specification);

    unset($table_specification['fields']);

    $introspect_index_schema = new \ReflectionMethod(get_class($this->schema), 'introspectIndexSchema');
    $index_schema = $introspect_index_schema->invoke($this->schema, $table_name);

    $this->assertEquals($table_specification$index_schema);
  }

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