commit ff6cea7497aceeb612ca8209448655cc4adae09e Author: Lee Rowlands Date: Fri Jan 11 12:25:41 2013 +1000 Work on tests passing diff --git a/core/modules/block/custom_block/custom_block.admin.inc b/core/modules/block/custom_block/custom_block.admin.inc index be1e32e..4fb5906 100644 --- a/core/modules/block/custom_block/custom_block.admin.inc +++ b/core/modules/block/custom_block/custom_block.admin.inc @@ -68,9 +68,19 @@ function custom_block_type_delete_form($form, &$form_state, CustomBlockType $blo '#value' => $block_type->id(), ); + $message = t('Are you sure you want to delete %label?', array('%label' => $block_type->label())); + + $blocks = entity_query('custom_block')->condition('type', $block_type->id())->execute(); + if (!empty($blocks)) { + drupal_set_title($message, PASS_THROUGH); + $caption = '

' . format_plural(count($blocks), '%label is used by 1 custom block on your site. You can not remove this block type until you have removed all of the %label blocks.', '%label is used by @count custom blocks on your site. You may not remove %label until you have removed all of the %label custom blocks.', array('%label' => $block_type->label())) . '

'; + $form['description'] = array('#markup' => $caption); + return $form; + } + return confirm_form( $form, - t('Are you sure you want to delete %label?', array('%label' => $block_type->label())), + $message, 'admin/structure/custom-blocks', t('This action cannot be undone.'), t('Delete') diff --git a/core/modules/block/custom_block/lib/Drupal/custom_block/CustomBlockRenderController.php b/core/modules/block/custom_block/lib/Drupal/custom_block/CustomBlockRenderController.php index eb227d3..c3d98b5 100644 --- a/core/modules/block/custom_block/lib/Drupal/custom_block/CustomBlockRenderController.php +++ b/core/modules/block/custom_block/lib/Drupal/custom_block/CustomBlockRenderController.php @@ -9,6 +9,7 @@ use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Entity\EntityRenderController; +use Drupal\entity\Plugin\Core\Entity\EntityDisplay; /** * Render controller for custom blocks. @@ -18,11 +19,11 @@ class CustomBlockRenderController extends EntityRenderController { /** * Overrides Drupal\Core\Entity\EntityRenderController::alterBuild(). */ - protected function alterBuild(array &$build, EntityInterface $entity, $view_mode, $langcode = NULL) { - parent::alterBuild($build, $entity, $view_mode, $langcode); + protected function alterBuild(array &$build, EntityInterface $entity, EntityDisplay $display, $view_mode, $langcode = NULL) { + parent::alterBuild($build, $entity, $display, $view_mode, $langcode); // Add contextual links for this custom block, except when the block. - if (!empty($entity->bid) && $view_mode == 'full') { - $build['#contextual_links']['custom_block'] = array('custom_block', array($entity->machine_name)); + if (!empty($entity->bid->value) && $view_mode == 'full') { + $build['#contextual_links']['custom_block'] = array('custom_block', array($entity->machine_name->value)); } } diff --git a/core/modules/block/custom_block/lib/Drupal/custom_block/CustomBlockStorageController.php b/core/modules/block/custom_block/lib/Drupal/custom_block/CustomBlockStorageController.php index 30f5265..963bd3f 100644 --- a/core/modules/block/custom_block/lib/Drupal/custom_block/CustomBlockStorageController.php +++ b/core/modules/block/custom_block/lib/Drupal/custom_block/CustomBlockStorageController.php @@ -48,6 +48,24 @@ protected function preSaveRevision(\stdClass $record, EntityInterface $entity) { } /** + * Overrides Drupal\Core\Entity\DatabaseStorageController::attachLoad(). + */ + protected function attachLoad(&$blocks, $load_revision = FALSE) { + // Create an array of block types for passing as a load argument. + // Note that blocks at this point are still \StdClass objects returned from + // the database. + foreach ($blocks as $id => $entity) { + $types[$entity->type] = $entity->type; + } + + // Besides the list of blocks, pass one additional argument to + // hook_custom_block_load(), containing a list of block types that were + // loaded. + $this->hookLoadArguments = array($types); + parent::attachLoad($blocks, $load_revision); + } + + /** * Overrides Drupal\Core\Entity\DatabaseStorageController::postSave(). */ function postSave(EntityInterface $block, $update) { diff --git a/core/modules/block/lib/Drupal/block/Tests/BlockTest.php b/core/modules/block/lib/Drupal/block/Tests/BlockTest.php index c607973..6ac4f16 100644 --- a/core/modules/block/lib/Drupal/block/Tests/BlockTest.php +++ b/core/modules/block/lib/Drupal/block/Tests/BlockTest.php @@ -108,23 +108,28 @@ public function testCustomBlock() { // Add a new custom block by filling out the input form on the admin/structure/block/add page. $info = strtolower($this->randomName(8)); + $values = array( + 'type' => 'basic', + 'info' => $info, + 'machine_name' => $info + ); + $block = entity_create('custom_block', $values)->save(); $custom_block['machine_name'] = $info; - $custom_block['info'] = $info; $custom_block['title'] = $this->randomName(8); - $custom_block['body[value]'] = $this->randomName(32); + $langcode = LANGUAGE_NOT_SPECIFIED; + $custom_block["block_body[$langcode][0][value]"] = $this->randomName(32); $custom_block['region'] = $this->regions[0]; - $this->drupalPost("admin/structure/block/list/block_plugin_ui:$default_theme/add/custom_blocks", $custom_block, t('Save block')); + $this->drupalPost("admin/structure/block/manage/custom_block:$info/add/custom_blocks", $custom_block, t('Save block')); $plugin_id = "plugin.core.block.$default_theme.$info"; $block = $manager->getInstance(array('config' => $plugin_id)); $config = $block->getConfig(); // Confirm that the custom block has been created, and then query the created bid. - $this->assertText(t('The block configuration has been saved.'), 'Custom block successfully created.'); + $this->assertText(t('The block configuration has been saved.'), 'Custom block instance successfully created.'); // Check that block_block_view() returns the correct title and content. $data = $block->build(); - $format = $config['format']; - $this->assertEqual(check_markup($custom_block['body[value]'], $format), render($data), 'BlockInterface::build() provides correct block content.'); + $this->assertRaw(check_markup($custom_block["block_body[$langcode][0][value]"], FILTER_DEFAULT), render($data), 'BlockInterface::build() provides correct block content.'); // Check whether the block can be moved to all available regions. $custom_block['module'] = 'block'; commit ef22deddc3e09ce0657bf6d3b3ff53e88e535333 Author: Lee Rowlands Date: Sun Jan 13 09:22:16 2013 +1000 Add text as dependency diff --git a/core/modules/block/custom_block/custom_block.info b/core/modules/block/custom_block/custom_block.info index 3dafb61..8d50a2f 100644 --- a/core/modules/block/custom_block/custom_block.info +++ b/core/modules/block/custom_block/custom_block.info @@ -4,3 +4,4 @@ package = Core version = VERSION core = 8.x dependencies[] = block +dependencies[] = text commit 1bbccb2c3793140cf8078e50ec102762bbbb21d4 Author: Lee Rowlands Date: Sun Jan 13 09:22:43 2013 +1000 Set default view mode, not full - full not always available in early install diff --git a/core/modules/block/custom_block/custom_block.module b/core/modules/block/custom_block/custom_block.module index 148ab06..bee1d7b 100644 --- a/core/modules/block/custom_block/custom_block.module +++ b/core/modules/block/custom_block/custom_block.module @@ -274,9 +274,9 @@ function custom_block_add_body_field($block_type_id, $label = 'Block body') { ); $instance = field_create_instance($instance); - // Assign display settings for the 'full'. - entity_get_display('custom_block', $block_type_id, 'full') - ->setComponent($field['field_name'], array( + // Assign display settings for 'default' view mode. + entity_get_display('custom_block', $block_type_id, 'default') + ->setComponent('block_body', array( 'label' => 'hidden', 'type' => 'text_default', )) commit 35cd81a7c691d621fd9bb3ce911611ac6958dd28 Author: Lee Rowlands Date: Sun Jan 13 09:22:57 2013 +1000 Get contextual links working diff --git a/core/modules/block/custom_block/lib/Drupal/custom_block/CustomBlockRenderController.php b/core/modules/block/custom_block/lib/Drupal/custom_block/CustomBlockRenderController.php index c3d98b5..422981c 100644 --- a/core/modules/block/custom_block/lib/Drupal/custom_block/CustomBlockRenderController.php +++ b/core/modules/block/custom_block/lib/Drupal/custom_block/CustomBlockRenderController.php @@ -23,7 +23,7 @@ protected function alterBuild(array &$build, EntityInterface $entity, EntityDisp parent::alterBuild($build, $entity, $display, $view_mode, $langcode); // Add contextual links for this custom block, except when the block. if (!empty($entity->bid->value) && $view_mode == 'full') { - $build['#contextual_links']['custom_block'] = array('custom_block', array($entity->machine_name->value)); + $build['#contextual_links']['custom_block'] = array('block', array($entity->machine_name->value)); } } commit b87ef32cd8065b1e5c09ac750f57121a99fab885 Author: Lee Rowlands Date: Sun Jan 13 09:23:18 2013 +1000 Only setup body field on block type create diff --git a/core/modules/block/custom_block/lib/Drupal/custom_block/CustomBlockTypeStorageController.php b/core/modules/block/custom_block/lib/Drupal/custom_block/CustomBlockTypeStorageController.php index 2e27b39..d9600c6 100644 --- a/core/modules/block/custom_block/lib/Drupal/custom_block/CustomBlockTypeStorageController.php +++ b/core/modules/block/custom_block/lib/Drupal/custom_block/CustomBlockTypeStorageController.php @@ -23,11 +23,11 @@ protected function postSave(EntityInterface $entity, $update) { if (!$update) { field_attach_create_bundle('custom_block', $entity->id()); + custom_block_add_body_field($entity->id()); } elseif ($entity->original->id() != $entity->id()) { field_attach_rename_bundle('custom_block', $entity->original->id(), $entity->id()); } - custom_block_add_body_field($entity->id()); } /** commit 799c458a356fe4e6e6ceafd265f95432fbd85e6c Author: Lee Rowlands Date: Sun Jan 13 09:23:35 2013 +1000 Don't set default langcode to string diff --git a/core/modules/block/custom_block/lib/Drupal/custom_block/Plugin/Core/Entity/CustomBlock.php b/core/modules/block/custom_block/lib/Drupal/custom_block/Plugin/Core/Entity/CustomBlock.php index aaba53d..8befb87 100644 --- a/core/modules/block/custom_block/lib/Drupal/custom_block/Plugin/Core/Entity/CustomBlock.php +++ b/core/modules/block/custom_block/lib/Drupal/custom_block/Plugin/Core/Entity/CustomBlock.php @@ -102,7 +102,7 @@ class CustomBlock extends EntityNG implements ContentEntityInterface { * * @var \Drupal\Core\Entity\Field\FieldInterface */ - public $langcode = LANGUAGE_NOT_SPECIFIED; + public $langcode; /** * The block description. commit 97de3902cc452bef3ebf4e11f66393b87da3de3c Author: Lee Rowlands Date: Sun Jan 13 09:23:48 2013 +1000 Entity BC changes required for viewing fields diff --git a/core/modules/field/field.attach.inc b/core/modules/field/field.attach.inc index 32a8c37..0cdbbf3 100644 --- a/core/modules/field/field.attach.inc +++ b/core/modules/field/field.attach.inc @@ -1341,6 +1341,9 @@ function field_attach_prepare_view($entity_type, array $entities, array $display // Add this entity to the items to be prepared. $prepare[$id] = $entity; + // Enable BC if necessary. + $entity = $entity->getBCEntity(); + // Determine the actual language code to display for each field, given the // language codes available in the field data. $options['langcode'][$id] = field_language($entity, NULL, $langcode); @@ -1412,6 +1415,8 @@ function field_attach_prepare_view($entity_type, array $entities, array $display * A renderable array for the field values. */ function field_attach_view(EntityInterface $entity, EntityDisplay $display, $langcode = NULL, array $options = array()) { + // Enable BC if necessary. + $entity = $entity->getBCEntity(); // Determine the actual language code to display for each field, given the // language codes available in the field data. $options['langcode'] = field_language($entity, NULL, $langcode); @@ -1424,6 +1429,9 @@ function field_attach_view(EntityInterface $entity, EntityDisplay $display, $lan $null = NULL; $output = field_invoke_method('view', $target_function, $entity, $null, $null, $options); + // Remove the BC layer now. + $entity = $entity->getOriginalEntity(); + // Let other modules alter the renderable array. $view_mode = $display->originalViewMode; $context = array( @@ -1459,6 +1467,8 @@ function field_attach_view(EntityInterface $entity, EntityDisplay $display, $lan * values. */ function field_attach_preprocess(EntityInterface $entity, $element, &$variables) { + // Enable BC if necessary. + $entity = $entity->getBCEntity(); foreach (field_info_instances($entity->entityType(), $entity->bundle()) as $instance) { $field_name = $instance['field_name']; if (isset($element[$field_name]['#language'])) { commit 60579147be50d5761935f64ae2c4cc383cc8a3e6 Author: Lee Rowlands Date: Sun Jan 13 09:24:10 2013 +1000 Added tests diff --git a/core/modules/block/custom_block/lib/Drupal/custom_block/Tests/CustomBlockBuildContentTest.php b/core/modules/block/custom_block/lib/Drupal/custom_block/Tests/CustomBlockBuildContentTest.php new file mode 100644 index 0000000..5ec715c --- /dev/null +++ b/core/modules/block/custom_block/lib/Drupal/custom_block/Tests/CustomBlockBuildContentTest.php @@ -0,0 +1,38 @@ + 'Rebuild content', + 'description' => 'Test the rebuilding of content for full view modes.', + 'group' => 'Custom Block', + ); + } + + /** + * Ensures that content is rebuilt in calls to custom_block_build_content(). + */ + function testCustomBlockRebuildContent() { + $block = $this->createCustomBlock(); + + // Set a property in the content array so we can test for its existence later on. + $block->content['test_content_property'] = array( + '#value' => $this->randomString(), + ); + $content = entity_view_multiple(array($block), 'full'); + + // If the property doesn't exist it means the block->content was rebuilt. + $this->assertFalse(isset($content['test_content_property']), 'Custom block content was emptied prior to being built.'); + } +} diff --git a/core/modules/block/custom_block/lib/Drupal/custom_block/Tests/CustomBlockCreationTest.php b/core/modules/block/custom_block/lib/Drupal/custom_block/Tests/CustomBlockCreationTest.php new file mode 100644 index 0000000..49e0b22 --- /dev/null +++ b/core/modules/block/custom_block/lib/Drupal/custom_block/Tests/CustomBlockCreationTest.php @@ -0,0 +1,100 @@ + 'Custom Block creation', + 'description' => 'Create a block and test saving it.', + 'group' => 'Custom Block', + ); + } + + function setUp() { + parent::setUp(); + $this->drupalLogin($this->admin_user); + } + + /** + * Creates a "Basic page" block and verifies its consistency in the database. + */ + function testCustomBlockCreation() { + // Create a block. + $edit = array(); + $langcode = LANGUAGE_NOT_SPECIFIED; + $edit['info'] = $this->randomName(8); + $edit['machine_name'] = drupal_strtolower($edit['info']); + $edit["block_body[$langcode][0][value]"] = $this->randomName(16); + $this->drupalPost('block/add/basic', $edit, t('Save')); + + // Check that the Basic block has been created. + $this->assertRaw(format_string('!block %name has been created.', array( + '!block' => 'Basic block', + '%name' => $edit["info"] + )), 'Basic block created.'); + + // Check that the block exists in the database. + $blocks = entity_load_multiple_by_properties('custom_block', array('info' => $edit['info'])); + $block = reset($blocks); + $this->assertTrue($block, 'Custom Block found in database.'); + } + + /** + * Verifies that a transaction rolls back the failed creation. + */ + function testFailedBlockCreation() { + // Create a block. + try { + $this->createCustomBlock('fail_creation'); + $this->fail('Expected exception has not been thrown.'); + } + catch (Exception $e) { + $this->pass('Expected exception has been thrown.'); + } + + if (Database::getConnection()->supportsTransactions()) { + // Check that the block does not exist in the database. + $bid = db_select('block_custom', 'b') + ->fields('b', array('bid')) + ->condition('info', 'fail_creation') + ->execute() + ->fetchField(); + $this->assertFalse($bid, 'Transactions supported, and block not found in database.'); + } + else { + // Check that the block exists in the database. + $bid = db_select('block_custom', 'b') + ->fields('b', array('bid')) + ->condition('info', 'fail_creation') + ->execute() + ->fetchField(); + $this->assertTrue($bid, 'Transactions not supported, and block found in database.'); + + // Check that the failed rollback was logged. + $records = db_query("SELECT wid FROM {watchdog} WHERE message LIKE 'Explicit rollback failed%'")->fetchAll(); + $this->assertTrue(count($records) > 0, 'Transactions not supported, and rollback error logged to watchdog.'); + } + } +} diff --git a/core/modules/block/custom_block/lib/Drupal/custom_block/Tests/CustomBlockLoadHooksTest.php b/core/modules/block/custom_block/lib/Drupal/custom_block/Tests/CustomBlockLoadHooksTest.php new file mode 100644 index 0000000..30b2203 --- /dev/null +++ b/core/modules/block/custom_block/lib/Drupal/custom_block/Tests/CustomBlockLoadHooksTest.php @@ -0,0 +1,67 @@ + 'Custom Block load hooks', + 'description' => 'Test the hooks invoked when a custom block is being loaded.', + 'group' => 'Custom Block', + ); + } + + /** + * Tests that hook_custom_block_load() is invoked correctly. + */ + function testHookCustomBlockLoad() { + $other_bundle = $this->createCustomBlockType('other'); + // Create some sample articles and pages. + $custom_block1 = $this->createCustomBlock(); + $custom_block2 = $this->createCustomBlock(); + $custom_block3 = $this->createCustomBlock(); + $custom_block4 = $this->createCustomBlock(FALSE, $other_bundle->id()); + + // Check that when a set of custom blocks that only contains basic blocks is + // loaded, the properties added to the custom block by + // custom_block_test_load_custom_block() correctly reflect the expected + // values. + $custom_blocks = entity_load_multiple_by_properties('custom_block', array('type' => 'basic')); + $loaded_custom_block = end($custom_blocks); + $this->assertEqual($loaded_custom_block->custom_block_test_loaded_bids, array( + $custom_block1->bid->value, + $custom_block2->bid->value, + $custom_block3->bid->value + ), 'hook_custom_block_load() received the correct list of custom_block IDs the first time it was called.'); + $this->assertEqual($loaded_custom_block->custom_block_test_loaded_types, array('basic'), 'hook_custom_block_load() received the correct list of custom block types the first time it was called.'); + + // Now, as part of the same page request, load a set of custom_blocks that contain + // both basic and other bundle, and make sure the parameters passed to + // custom_block_test_custom_block_load() are correctly updated. + $custom_blocks = entity_load_multiple('custom_block', entity_query('custom_block')->execute(), TRUE); + $loaded_custom_block = end($custom_blocks); + $this->assertEqual($loaded_custom_block->custom_block_test_loaded_bids, array( + $custom_block1->bid->value, + $custom_block2->bid->value, + $custom_block3->bid->value, + $custom_block4->bid->value + ), 'hook_custom_block_load() received the correct list of custom_block IDs the second time it was called.'); + $this->assertEqual($loaded_custom_block->custom_block_test_loaded_types, array('basic', 'other'), 'hook_custom_block_load() received the correct list of custom_block types the second time it was called.'); + } +} diff --git a/core/modules/block/custom_block/lib/Drupal/custom_block/Tests/CustomBlockRevisionsTest.php b/core/modules/block/custom_block/lib/Drupal/custom_block/Tests/CustomBlockRevisionsTest.php new file mode 100644 index 0000000..2497ff5 --- /dev/null +++ b/core/modules/block/custom_block/lib/Drupal/custom_block/Tests/CustomBlockRevisionsTest.php @@ -0,0 +1,86 @@ + 'Custom Block revisions', + 'description' => 'Create a block with revisions.', + 'group' => 'Custom Block', + ); + } + + function setUp() { + parent::setUp(); + + // Create initial block. + $block = $this->createCustomBlock('initial'); + + $blocks = array(); + $logs = array(); + + // Get original block. + $blocks[] = $block->vid->value; + $logs[] = ''; + + // Create three revisions. + $revision_count = 3; + for ($i = 0; $i < $revision_count; $i++) { + $block->setNewRevision(TRUE); + $logs[] = $block->log->value = $this->randomName(32); + $block->save(); + $blocks[] = $block->vid->value; + } + + $this->blocks = $blocks; + $this->logs = $logs; + } + + /** + * Checks block revision related operations. + */ + function testRevisions() { + $blocks = $this->blocks; + $logs = $this->logs; + + foreach ($blocks as $delta => $vid) { + // Confirm the correct revision text appears. + $loaded = entity_revision_load('custom_block', $vid); + // Verify log is the same. + $this->assertEqual($loaded->log->value, $logs[$delta], format_string('Correct log message found for revision !revision', array( + '!revision' => $loaded->vid->value + ))); + } + + // Confirm that this is the default revision. + $this->assertTrue($loaded->isDefaultRevision(), 'Third block revision is the default one.'); + + // Make a new revision and set it to not be default. + // This will create a new revision that is not "front facing". + // Save this as a non-default revision. + $loaded->setNewRevision(); + $loaded->isDefaultRevision = FALSE; + $loaded->block_body = $this->randomName(8); + $loaded->save(); + + $this->drupalGet('block/' . $loaded->bid->value); + $this->assertNoText($loaded->block_body->value, 'Revision body text is not present on default version of block.'); + + // Verify that the non-default revision vid is greater than the default + // revision vid. + $default_revision = entity_load('custom_block', $loaded->bid->value); + $this->assertTrue($loaded->vid->value > $default_revision->vid->value, 'Revision vid is greater than default revision vid.'); + } +} diff --git a/core/modules/block/custom_block/lib/Drupal/custom_block/Tests/CustomBlockSaveTest.php b/core/modules/block/custom_block/lib/Drupal/custom_block/Tests/CustomBlockSaveTest.php new file mode 100644 index 0000000..077294b --- /dev/null +++ b/core/modules/block/custom_block/lib/Drupal/custom_block/Tests/CustomBlockSaveTest.php @@ -0,0 +1,103 @@ + 'Custom Block save', + 'description' => 'Test $custom_block->save() for saving content.', + 'group' => 'Custom Block', + ); + } + + function setUp() { + parent::setUp(); + $this->drupalLogin($this->admin_user); + } + + /** + * Checks whether custom block IDs are saved properly during an import. + */ + function testImport() { + // Custom block ID must be a number that is not in the database. + $max_bid = db_query('SELECT MAX(bid) FROM {block_custom}')->fetchField(); + $test_bid = $max_bid + mt_rand(1000, 1000000); + $info = $this->randomName(8); + $block = array( + 'info' => $info, + 'machine_name' => $info, + 'block_body' => array(LANGUAGE_NOT_SPECIFIED => array(array('value' => $this->randomName(32)))), + 'type' => 'basic', + 'bid' => $test_bid + ); + $block = entity_create('custom_block', $block); + $block->enforceIsNew(TRUE); + $block->save(); + + // Verify that block_submit did not wipe the provided bid. + $this->assertEqual($block->bid->value, $test_bid, 'Block imported using provide bid'); + + // Test the import saved. + $block_by_bid = custom_block_load($test_bid); + $this->assertTrue($block_by_bid, 'Custom block load by block ID.'); + } + + /** + * Tests determing changes in hook_block_presave() and verifies the static + * block load cache is cleared upon save. + */ + function testDeterminingChanges() { + // Initial creation. + $block = $this->createCustomBlock('test_changes'); + + // Update the block without applying changes. + $block->save(); + $this->assertEqual($block->label(), 'test_changes', 'No changes have been determined.'); + + // Apply changes. + $block->info->value = 'updated'; + $block->save(); + + // The hook implementations custom_block_test_custom_block_presave() and + // custom_block_test_custom_block_update() determine changes and change the + // title. + $this->assertEqual($block->label(), 'updated_presave_update', 'Changes have been determined.'); + + // Test the static block load cache to be cleared. + $block = custom_block_load($block->bid->value); + $this->assertEqual($block->label(), 'updated_presave', 'Static cache has been cleared.'); + } + + /** + * Tests saving a block on block insert. + * + * This test ensures that a block has been fully saved when + * hook_custom_block_insert() is invoked, so that the block can be saved again + * in a hook implementation without errors. + * + * @see block_test_block_insert() + */ + function testCustomBlockSaveOnInsert() { + // custom_block_test_custom_block_insert() tiggers a save on insert if the + // title equals 'new'. + $block = $this->createCustomBlock('new'); + $this->assertEqual($block->label(), 'CustomBlock ' . $block->bid->value, 'Custom block saved on block insert.'); + } +} diff --git a/core/modules/block/custom_block/lib/Drupal/custom_block/Tests/CustomBlockTestBase.php b/core/modules/block/custom_block/lib/Drupal/custom_block/Tests/CustomBlockTestBase.php new file mode 100644 index 0000000..aa11305 --- /dev/null +++ b/core/modules/block/custom_block/lib/Drupal/custom_block/Tests/CustomBlockTestBase.php @@ -0,0 +1,92 @@ +admin_user = $this->drupalCreateUser($this->permissions); + } + + /** + * Creates a custom block. + * + * @param string $title + * (optional) Title of block. When no value is given uses a random name. + * Defaults to FALSE. + * @param string $bundle + * (optional) Bundle name. Defaults to 'basic'. + * + * @return Drupal\custom_block\Plugin\Core\Entity\CustomBlock + * Created custom block. + */ + function createCustomBlock($title = FALSE, $bundle = 'basic') { + $title = ($title ? : $this->randomName()); + if ($custom_block = entity_create('custom_block', array( + 'machine_name' => $title, + 'info' => $title, + 'type' => $bundle, + 'langcode' => 'en' + ))) { + $custom_block->save(); + } + return $custom_block; + } + + /** + * Creates a custom block type (bundle). + * + * @param string $label + * The block type label. + * + * @return Drupal\custom_block\Plugin\Core\Entity\CustomBlockType + * Created custom block type. + */ + function createCustomBlockType($label) { + $bundle = entity_create('custom_block_type', array( + 'id' => $label, + 'label' => $label, + 'revision' => FALSE + )); + $bundle->save(); + return $bundle; + } + +} diff --git a/core/modules/block/custom_block/lib/Drupal/custom_block/Tests/CustomBlockTranslationUITest.php b/core/modules/block/custom_block/lib/Drupal/custom_block/Tests/CustomBlockTranslationUITest.php new file mode 100644 index 0000000..7c6513b --- /dev/null +++ b/core/modules/block/custom_block/lib/Drupal/custom_block/Tests/CustomBlockTranslationUITest.php @@ -0,0 +1,144 @@ + 'Custom Block translation UI', + 'description' => 'Tests the node translation UI.', + 'group' => 'Custom Block', + ); + } + + /** + * Overrides \Drupal\simpletest\WebTestBase::setUp(). + */ + function setUp() { + $this->entityType = 'custom_block'; + $this->bundle = 'basic'; + $this->name = drupal_strtolower($this->randomName()); + parent::setUp(); + } + + /** + * Overrides \Drupal\translation_entity\Tests\EntityTranslationUITest::getTranslatorPermission(). + */ + function getTranslatorPermissions() { + return array('translate any entity', 'edit original values', 'access administration pages', 'administer blocks'); + } + + /** + * Creates a custom block. + * + * @param string $title + * (optional) Title of block. When no value is given uses a random name. + * Defaults to FALSE. + * @param string $bundle + * (optional) Bundle name. When no value is given, defaults to + * $this->bundle. Defaults to FALSE. + * + * @return Drupal\custom_block\Plugin\Core\Entity\CustomBlock + * Created custom block. + */ + function createCustomBlock($title = FALSE, $bundle = FALSE) { + $title = ($title ? : $this->randomName()); + $bundle = ($bundle ? : $this->bundle); + $custom_block = entity_create('custom_block', array( + 'machine_name' => $title, + 'info' => $title, + 'type' => $bundle, + 'langcode' => 'en' + )); + $custom_block->save(); + return $custom_block; + } + + /** + * Tests field translation form. + */ + function testFieldTranslationForm() { + $admin_user = $this->drupalCreateUser(array('translate any entity', 'access administration pages', 'administer blocks')); + $this->drupalLogin($admin_user); + + $block = $this->createCustomBlock($this->name); + + // Visit translation page. + $this->drupalGet('block/' . $block->machine_name->value . '/translations'); + $this->assertRaw('Not translated'); + + // Delete the only translatable field. + field_delete_field('field_test_et_ui_test'); + + // Visit translation page. + $this->drupalGet('block/' . $block->machine_name->value . '/translations'); + $this->assertRaw('no translatable fields'); + } + + /** + * Overrides \Drupal\translation_entity\Tests\EntityTranslationUITest::getNewEntityValues(). + */ + protected function getNewEntityValues($langcode) { + return array('machine_name' => $this->name, 'info' => $this->name) + parent::getNewEntityValues($langcode); + } + + /** + * Test that no metadata is stored for a disabled bundle. + */ + public function testDisabledBundle() { + // Create a bundle that does not have translation enabled. + $disabledBundle = $this->randomName(); + $bundle = entity_create('custom_block_type', array( + 'id' => $disabledBundle, + 'label' => $disabledBundle, + 'revision' => FALSE + )); + $bundle->save(); + + // Create a node for each bundle. + $enabledCustomBlock = $this->createCustomBlock(); + $disabledCustomBlock = $this->createCustomBlock(FALSE, $bundle->id()); + + // Make sure that only a single row was inserted into the + // {translation_entity} table. + $rows = db_query('SELECT * FROM {translation_entity}')->fetchAll(); + $this->assertEqual(1, count($rows)); + $this->assertEqual($enabledCustomBlock->id(), reset($rows)->entity_id); + } + + /** + * Overrides \Drupal\translation_entity\Tests\EntityTranslationUITest::getEditValues(). + */ + protected function getEditValues($values, $langcode, $new = FALSE) { + $edit = parent::getEditValues($values, $langcode, $new); + // Can't submit machine name if not new. + if (!$new) { + unset($edit['machine_name']); + } + return $edit; + } +} diff --git a/core/modules/block/custom_block/lib/Drupal/custom_block/Tests/CustomBlockTypeTest.php b/core/modules/block/custom_block/lib/Drupal/custom_block/Tests/CustomBlockTypeTest.php new file mode 100644 index 0000000..805824d --- /dev/null +++ b/core/modules/block/custom_block/lib/Drupal/custom_block/Tests/CustomBlockTypeTest.php @@ -0,0 +1,123 @@ + 'CustomBlock types', + 'description' => 'Ensures that custom block type functions work correctly.', + 'group' => 'Custom Block', + ); + } + + /** + * Tests creating a block type programmatically and via a form. + */ + function testCustomBlockTypeCreation() { + // Create a block type programmaticaly. + $type = $this->createCustomBlockType('other'); + + $block_type = entity_load('custom_block_type', 'other'); + $this->assertTrue($block_type, 'The new block type has been created.'); + + // Login a test user. + $this->drupalLogin($this->admin_user); + + $this->drupalGet('block/add/' . $type->id()); + $this->assertResponse(200, 'The new block type can be accessed at bloack/add.'); + + // Create a block type via the user interface. + $edit = array( + 'id' => 'foo', + 'label' => 'title for foo', + ); + $this->drupalPost('admin/structure/custom-blocks/add', $edit, t('Save')); + $block_type = entity_load('custom_block_type', 'foo'); + $this->assertTrue($block_type, 'The new block type has been created.'); + } + + /** + * Tests editing a block type using the UI. + */ + function testCustomBlockTypeEditing() { + $this->drupalLogin($this->admin_user); + // We need two block types to prevent /block/add redirecting. + $this->createCustomBlockType('other'); + + $instance = field_info_instance('custom_block', 'block_body', 'basic'); + $this->assertEqual($instance['label'], 'Block body', 'Body field was found.'); + + // Verify that title and body fields are displayed. + $this->drupalGet('block/add/basic'); + $this->assertRaw('Block description', 'Block info field was found.'); + $this->assertRaw('Block body', 'Body field was found.'); + + // Change the block type name. + $edit = array( + 'label' => 'Bar', + ); + $this->drupalPost('admin/structure/custom-blocks/manage/basic', $edit, t('Save')); + field_info_cache_clear(); + + $this->drupalGet('block/add'); + $this->assertRaw('Bar', 'New name was displayed.'); + $this->clickLink('Bar'); + $this->assertEqual(url('block/add/basic', array('absolute' => TRUE)), $this->getUrl(), 'Original machine name was used in URL.'); + + // Remove the body field. + $this->drupalPost('admin/structure/custom-blocks/manage/basic/fields/block_body/delete', array(), t('Delete')); + // Resave the settings for this type. + $this->drupalPost('admin/structure/custom-blocks/manage/basic', array(), t('Save')); + // Check that the body field doesn't exist. + $this->drupalGet('block/add/basic'); + $this->assertNoRaw('Block body', 'Body field was not found.'); + } + + /** + * Tests deleting a block type that still has content. + */ + function testCustomBlockTypeDeletion() { + // Create a block type programmatically. + $type = $this->createCustomBlockType('foo'); + + $this->drupalLogin($this->admin_user); + + // Add a new block of this type. + $block = $this->createCustomBlock(FALSE, 'foo'); + // Attempt to delete the block type, which should not be allowed. + $this->drupalGet('admin/structure/custom-blocks/manage/' . $type->id() . '/delete'); + $this->assertRaw( + t('%label is used by 1 custom block on your site. You can not remove this block type until you have removed all of the %label blocks.', array('%label' => $type->label())), + 'The block type will not be deleted until all blocks of that type are removed.' + ); + $this->assertNoText(t('This action cannot be undone.'), 'The node type deletion confirmation form is not available.'); + + // Delete the block. + $block->delete(); + // Attempt to delete the block type, which should now be allowed. + $this->drupalGet('admin/structure/custom-blocks/manage/' . $type->id() . '/delete'); + $this->assertRaw( + t('Are you sure you want to delete %type?', array('%type' => $type->id())), + 'The block type is available for deletion.' + ); + $this->assertText(t('This action cannot be undone.'), 'The custom block type deletion confirmation form is available.'); + } + +} diff --git a/core/modules/block/custom_block/lib/Drupal/custom_block/Tests/CustomBlockUnitTestBase.php b/core/modules/block/custom_block/lib/Drupal/custom_block/Tests/CustomBlockUnitTestBase.php new file mode 100644 index 0000000..f3e2440 --- /dev/null +++ b/core/modules/block/custom_block/lib/Drupal/custom_block/Tests/CustomBlockUnitTestBase.php @@ -0,0 +1,76 @@ +randomName()); + $custom_block = entity_create('custom_block', array( + 'machine_name' => $title, + 'info' => $title, + 'type' => $bundle, + 'langcode' => 'en' + )); + $custom_block->save(); + return $custom_block; + } + + /** + * Creates a custom block type (bundle). + * + * @param string $label + * The block type label. + * + * @return Drupal\custom_block\Plugin\Core\Entity\CustomBlockType + * Created custom block type. + */ + function createCustomBlockType($label) { + $bundle = entity_create('custom_block_type', array( + 'id' => $label, + 'label' => $label, + 'revision' => FALSE + )); + $bundle->save(); + return $bundle; + } + +} diff --git a/core/modules/block/custom_block/lib/Drupal/custom_block/Tests/PageEditTest.php b/core/modules/block/custom_block/lib/Drupal/custom_block/Tests/PageEditTest.php new file mode 100644 index 0000000..bd00e45 --- /dev/null +++ b/core/modules/block/custom_block/lib/Drupal/custom_block/Tests/PageEditTest.php @@ -0,0 +1,86 @@ + 'Custom Block edit', + 'description' => 'Create a block and test block edit functionality.', + 'group' => 'Custom Block', + ); + } + + function setUp() { + $this->permissions[] = 'view custom blocks'; + parent::setUp(); + } + + /** + * Checks block edit functionality. + */ + function testPageEdit() { + $this->drupalLogin($this->admin_user); + + $langcode = LANGUAGE_NOT_SPECIFIED; + $title_key = 'info'; + $body_key = "block_body[$langcode][0][value]"; + // Create block to edit. + $edit = array(); + $edit['info'] = $edit['machine_name'] = drupal_strtolower($this->randomName(8)); + $edit[$body_key] = $this->randomName(16); + $this->drupalPost('block/add/basic', $edit, t('Save')); + + // Check that the block exists in the database. + $blocks = entity_query('custom_block')->condition('info', $edit['info'])->execute(); + $block = entity_load('custom_block', reset($blocks)); + $this->assertTrue($block, 'Custom block found in database.'); + + // Check that "edit" link points to correct page. + $this->drupalGet('block/'. $block->machine_name->value); + $this->clickLink(t('Edit')); + $edit_url = url("block/" . $block->machine_name->value . "/edit", array('absolute' => TRUE)); + $actual_url = $this->getURL(); + $this->assertEqual($edit_url, $actual_url, 'On edit page.'); + + // Check that the title and body fields are displayed with the correct values. + $active = '' . t('(active tab)') . ''; + $link_text = t('!local-task-title!active', array('!local-task-title' => t('Edit'), '!active' => $active)); + $this->assertText(strip_tags($link_text), 0, 'Edit tab found and marked active.'); + $this->assertFieldByName($title_key, $edit[$title_key], 'Title field displayed.'); + $this->assertFieldByName($body_key, $edit[$body_key], 'Body field displayed.'); + + // Edit the content of the block. + $edit = array(); + $edit[$title_key] = $this->randomName(8); + $edit[$body_key] = $this->randomName(16); + // Stay on the current page, without reloading. + $this->drupalPost(NULL, $edit, t('Save')); + + // Check that the title and body fields are displayed with the updated values. + $this->drupalGet('block/'. $block->machine_name->value); + $this->assertText($edit[$title_key], 'Title displayed.'); + $this->assertText($edit[$body_key], 'Body displayed.'); + + // Edit the same block, creating a new revision. + $this->drupalGet("block/" . $block->machine_name->value . "/edit"); + $edit = array(); + $edit['info'] = $this->randomName(8); + $edit[$body_key] = $this->randomName(16); + $edit['revision'] = TRUE; + $this->drupalPost(NULL, $edit, t('Save')); + + // Ensure that the block revision has been created. + $revised_block = entity_load('custom_block', $block->bid->value, TRUE); + $this->assertNotIdentical($block->vid->value, $revised_block->vid->value, 'A new revision has been created.'); + } +} diff --git a/core/modules/block/custom_block/tests/modules/custom_block_test/custom_block_test.info b/core/modules/block/custom_block/tests/modules/custom_block_test/custom_block_test.info new file mode 100644 index 0000000..9993892 --- /dev/null +++ b/core/modules/block/custom_block/tests/modules/custom_block_test/custom_block_test.info @@ -0,0 +1,5 @@ +name = "Custom Block module tests" +description = "Support module for custom block related testing." +package = Testing +core = 8.x +hidden = TRUE diff --git a/core/modules/block/custom_block/tests/modules/custom_block_test/custom_block_test.module b/core/modules/block/custom_block/tests/modules/custom_block_test/custom_block_test.module new file mode 100644 index 0000000..83e3f4f --- /dev/null +++ b/core/modules/block/custom_block/tests/modules/custom_block_test/custom_block_test.module @@ -0,0 +1,82 @@ +custom_block_test_loaded_bids = $bids; + $custom_block->custom_block_test_loaded_types = $types; + } +} + +/** + * Implements hook_custom_block_view(). + */ +function custom_block_test_custom_block_view(CustomBlock $custom_block, $view_mode) { + // Add extra content. + $custom_block->content['extra_content'] = array( + '#markup' => 'Yowser', + ); +} + +/** + * Implements hook_custom_block_presave(). + */ +function custom_block_test_custom_block_presave(CustomBlock $custom_block) { + if ($custom_block->info->value == 'testing_custom_block_presave') { + $custom_block->info->value .= '_presave'; + } + // Determine changes. + if (!empty($custom_block->original) && $custom_block->original->info->value == 'test_changes') { + if ($custom_block->original->info->value != $custom_block->info->value) { + $custom_block->info->value .= '_presave'; + } + } +} + +/** + * Implements hook_custom_block_update(). + */ +function custom_block_test_custom_block_update(CustomBlock $custom_block) { + // Determine changes on update. + if (!empty($custom_block->original) && $custom_block->original->info->value == 'test_changes') { + if ($custom_block->original->info->value != $custom_block->info->value) { + $custom_block->info->value .= '_update'; + } + } +} + +/** + * Implements hook_custom_block_insert(). + * + * This tests saving a custom_block on custom_block insert. + * + * @see \Drupal\custom_block\Tests\CustomBlockSaveTest::testCustomBlockSaveOnInsert() + */ +function custom_block_test_custom_block_insert(CustomBlock $custom_block) { + // Set the custom_block title to the custom_block ID and save. + if ($custom_block->info->value == 'new') { + $custom_block->info->value = 'CustomBlock '. $custom_block->bid->value; + $custom_block->save(); + } + if ($custom_block->info->value == 'fail_creation') { + throw new Exception('Test exception for rollback.'); + } +} commit 68ee443b146e27bc3157b2d7946967a96ef2410d Author: Lee Rowlands Date: Sun Jan 13 09:47:21 2013 +1000 Remove unit test base, not used diff --git a/core/modules/block/custom_block/lib/Drupal/custom_block/Tests/CustomBlockUnitTestBase.php b/core/modules/block/custom_block/lib/Drupal/custom_block/Tests/CustomBlockUnitTestBase.php deleted file mode 100644 index f3e2440..0000000 --- a/core/modules/block/custom_block/lib/Drupal/custom_block/Tests/CustomBlockUnitTestBase.php +++ /dev/null @@ -1,76 +0,0 @@ -randomName()); - $custom_block = entity_create('custom_block', array( - 'machine_name' => $title, - 'info' => $title, - 'type' => $bundle, - 'langcode' => 'en' - )); - $custom_block->save(); - return $custom_block; - } - - /** - * Creates a custom block type (bundle). - * - * @param string $label - * The block type label. - * - * @return Drupal\custom_block\Plugin\Core\Entity\CustomBlockType - * Created custom block type. - */ - function createCustomBlockType($label) { - $bundle = entity_create('custom_block_type', array( - 'id' => $label, - 'label' => $label, - 'revision' => FALSE - )); - $bundle->save(); - return $bundle; - } - -}