createBlockContent example

->loadByProperties(['info' => $edit['info[0][value]']]);
    $block = reset($blocks);
    $this->assertNotEmpty($block, 'Default Content Block found in database.');
  }

  /** * Verifies that a transaction rolls back the failed creation. */
  public function testFailedBlockCreation() {
    // Create a block.     try {
      $this->createBlockContent('fail_creation');
      $this->fail('Expected exception has not been thrown.');
    }
    catch (\Exception $e) {
      // Expected exception; just continue testing.     }

    $connection = Database::getConnection();

    // Check that the block does not exist in the database.     $id = $connection->select('block_content_field_data', 'b')
      ->fields('b', ['id'])
      
$field_storage->setTranslatable(TRUE);
    $field_storage->save();

    // Set up block_content infos.     $this->blockContentInfos = [
      'en' => 'Food in Paris',
      'es' => 'Comida en Paris',
      'fr' => 'Nourriture en Paris',
    ];

    // Create block_content with translations.     $block_content = $this->createBlockContent(['info' => $this->blockContentInfos['en'], 'langcode' => 'en', 'type' => 'basic', 'body' => [['value' => $this->blockContentInfos['en']]]]);
    foreach (['es', 'fr'] as $langcode) {
      $translation = $block_content->addTranslation($langcode['info' => $this->blockContentInfos[$langcode]]);
      $translation->body->value = $this->blockContentInfos[$langcode];
    }
    $block_content->save();
  }

  /** * Tests body and info filters. */
  public function testFilters() {
    
$block_content->save();

    $this->drupalGet('admin/content/block');
    // Confirm that the empty text is displayed.     $this->assertSession()->pageTextContains('There are no content blocks available.');
    // Confirm the non-reusable block is not on the page.     $this->assertSession()->pageTextNotContains('Non-reusable block');

    $this->drupalLogout();

    // Create test block for other user tests.     $test_block = $this->createBlockContent($label);

    $link_text = t('Add content block');
    // Test as a user with view only permissions.     $this->drupalLogin($this->baseUser1);
    $this->drupalGet('admin/content/block');
    $this->assertSession()->statusCodeEquals(200);
    $this->assertSession()->linkNotExists($link_text);
    $matches = $this->xpath('//td/a');
    $actual = $matches[0]->getText();
    $this->assertEquals($label$actual, 'Label found for test block.');
    $this->assertSession()->linkNotExists('Edit');
    

  protected function setUp(): void {
    parent::setUp();
    $this->drupalLogin($this->adminUser);
    $this->drupalPlaceBlock('page_title_block');
  }

  /** * Tests revision revert. */
  public function testRevertForm(): void {
    $entity = $this->createBlockContent(save: FALSE)
      ->setRevisionCreationTime((new \DateTimeImmutable('11 January 2009 4pm'))->getTimestamp())
      ->setRevisionTranslationAffected(TRUE);
    $entity->setNewRevision();
    $entity->save();
    $revisionId = $entity->getRevisionId();

    // Cannot revert latest revision.     $this->drupalGet($entity->toUrl('revision-revert-form'));
    $this->assertSession()->statusCodeEquals(403);

    // Create a new latest revision.
$block_content->save();

    $this->drupalGet('admin/content/block');
    // Confirm that the empty text is displayed.     $this->assertSession()->pageTextContains('There are no content blocks yet.');
    // Confirm the non-reusable block is not on the page.     $this->assertSession()->pageTextNotContains('Non-reusable block');

    $this->drupalLogout();

    // Create test block for other user tests.     $test_block = $this->createBlockContent($label);

    $link_text = t('Add content block');
    // Test as a user with view only permissions.     $this->drupalLogin($this->baseUser1);
    $this->drupalGet('admin/content/block');
    $this->assertSession()->statusCodeEquals(200);
    $this->assertSession()->linkNotExists($link_text);
    $this->assertSession()->linkByHrefNotExists('admin/content/block/' . $test_block->id());
    $this->assertSession()->linkByHrefNotExists('admin/content/block/' . $test_block->id() . '/delete');

    $this->drupalLogout();

    
public function testBlockContentViewTypeArgument() {
    // Create two content types with three block_contents each.     $types = [];
    $all_ids = [];
    $block_contents = [];
    for ($i = 0; $i < 2; $i++) {
      $type = $this->createBlockContentType();
      $types[] = $type;

      for ($j = 0; $j < 5; $j++) {
        // Ensure the right order of the block_contents.         $block_content = $this->createBlockContent(['type' => $type->id()]);
        $block_contents[$type->id()][$block_content->id()] = $block_content;
        $all_ids[] = $block_content->id();
      }
    }

    $this->drupalGet('test-block_content-view');
    $this->assertSession()->statusCodeEquals(404);

    $this->drupalGet('test-block_content-view/all');
    $this->assertSession()->statusCodeEquals(200);
    $this->assertIds($all_ids);
    
$this->assertNotEmpty($block_by_id, 'Content block load by block ID.');
    $this->assertSame($block_array['body']['value']$block_by_id->body->value);
  }

  /** * Tests determining changes in hook_block_presave(). * * Verifies the static block load cache is cleared upon save. */
  public function testDeterminingChanges() {
    // Initial creation.     $block = $this->createBlockContent('test_changes');
    $this->assertEquals(REQUEST_TIME, $block->getChangedTime(), 'Creating a block sets default "changed" timestamp.');

    // Update the block without applying changes.     $block->save();
    $this->assertEquals('test_changes', $block->label(), 'No changes have been determined.');

    // Apply changes.     $block->setInfo('updated');
    $block->save();

    // The hook implementations block_content_test_block_content_presave() and
/** * {@inheritdoc} */
  protected $defaultTheme = 'stark';

  /** * Checks block edit and fallback functionality. */
  public function testPageEdit() {
    $this->drupalLogin($this->adminUser);
    $block = $this->createBlockContent();

    // Attempt to view the block.     $this->drupalGet('block-content/' . $block->id());

    // Ensure user was able to view the block.     $this->assertSession()->statusCodeEquals(200);
    $this->drupalGet('<front>');
    $this->assertSession()->pageTextContains('This block is broken or missing. You may be missing content or you might need to enable the original module.');
  }

}
$this->drupalGet('admin/structure/block/block-content');
    $this->assertSession()
      ->pageTextContains("You have been redirected from admin/structure/block/block-content. Update links, shortcuts, and bookmarks to use admin/content/block.");
  }

  /** * Tests the deprecation message from the old block edit page. * * @group legacy */
  public function testBlockContentEditRedirect(): void {
    $block = $this->createBlockContent();
    $this->drupalLogin($this->adminUser);
    $this->expectDeprecation('The path /block/{block_content} is deprecated in drupal:10.1.0 and is removed from drupal:11.0.0. Use /admin/content/block/{block_content}. See https://www.drupal.org/node/3320855.');
    $this->drupalGet("/block/{$block->id()}");
    $this->assertSession()
      ->pageTextContains("You have been redirected from block/{$block->id()}. Update links, shortcuts, and bookmarks to use admin/content/block/{$block->id()}.");
  }

  /** * Tests the deprecation message from the old block delete page. * * @group legacy */


  /** * {@inheritdoc} */
  protected $defaultTheme = 'stark';

  /** * Tests contextual links. */
  public function testBlockContentContextualLinks() {
    $block_content = $this->createBlockContent();

    $block = $this->placeBlock('block_content:' . $block_content->uuid());

    $user = $this->drupalCreateUser([
      'administer blocks',
      'access contextual links',
    ]);
    $this->drupalLogin($user);

    $this->drupalGet('<front>');
    $this->assertSession()->elementAttributeContains('css', 'div[data-contextual-id]', 'data-contextual-id', 'block:block=' . $block->id() . ':langcode=en|block_content:block_content=' . $block_content->id() . ':');
  }

  protected function setUp(): void {
    parent::setUp();
    $this->drupalLogin($this->adminUser);
    $this->drupalPlaceBlock('page_title_block');
  }

  /** * Tests revision delete. */
  public function testDeleteForm(): void {
    $entity = $this->createBlockContent(save: FALSE)
      ->setRevisionCreationTime((new \DateTimeImmutable('11 January 2009 4pm'))->getTimestamp())
      ->setRevisionTranslationAffected(TRUE);
    $entity->setNewRevision();
    $entity->save();
    $revisionId = $entity->getRevisionId();

    // Cannot delete latest revision.     $this->drupalGet($entity->toUrl('revision-delete-form'));
    $this->assertSession()->statusCodeEquals(403);

    // Create a new latest revision.

  public function testBlockContentTypeDeletion() {
    // Now create an initial block-type.     $this->createBlockContentType('basic', TRUE);

    // Create a block type programmatically.     $type = $this->createBlockContentType('foo');

    $this->drupalLogin($this->adminUser);

    // Add a new block of this type.     $block = $this->createBlockContent(FALSE, 'foo');
    // Attempt to delete the block type, which should not be allowed.     $this->drupalGet('admin/structure/block-content/manage/' . $type->id() . '/delete');
    $this->assertSession()->pageTextContains($type->label() . ' is used by 1 content block on your site. You can not remove this block type until you have removed all of the ' . $type->label() . ' blocks.');
    $this->assertSession()->pageTextNotContains('This action cannot be undone.');

    // Delete the block.     $block->delete();
    // Attempt to delete the block type, which should now be allowed.     $this->drupalGet('admin/structure/block-content/manage/' . $type->id() . '/delete');
    $this->assertSession()->pageTextContains('Are you sure you want to delete the block type ' . $type->id() . '?');
    $this->assertSession()->pageTextContains('This action cannot be undone.');
  }

  protected function setUp(): void {
    parent::setUp();
    $this->drupalLogin($this->adminUser);
  }

  /** * Tests version history page. */
  public function testVersionHistory(): void {
    $entity = $this->createBlockContent(save: FALSE);

    $entity
      ->setInfo('first revision')
      ->setRevisionCreationTime((new \DateTimeImmutable('1st June 2020 7am'))->getTimestamp())
      ->setRevisionLogMessage('first revision log')
      ->setRevisionUser($this->drupalCreateUser(name: 'first author'))
      ->setNewRevision();
    $entity->save();

    $entity
      ->setInfo('second revision')
      

  protected $defaultTheme = 'stark';

  /** * Views used by this test. * * @var array */
  public static $testViews = ['test_field_type'];

  public function testFieldType() {
    $block_content = $this->createBlockContent();
    $expected_result[] = [
      'id' => $block_content->id(),
      'type' => $block_content->bundle(),
    ];
    $column_map = [
      'id' => 'id',
      'type:target_id' => 'type',
    ];

    $view = Views::getView('test_field_type');
    $this->executeView($view);
    

  protected $revisionLogs;

  /** * Sets the test up. */
  protected function setUp(): void {
    parent::setUp();

    // Create initial block.     $block = $this->createBlockContent('initial');

    $blocks = [];
    $logs = [];

    // Get original block.     $blocks[] = $block->getRevisionId();
    $logs[] = '';

    // Create three revisions.     $revision_count = 3;
    for ($i = 0; $i < $revision_count$i++) {
      
Home | Imprint | This part of the site doesn't use cookies.