【发布时间】:2018-08-08 00:00:15
【问题描述】:
我在 Drupal 8 中使用自定义模块创建了 Block。
是否可以用 PHPUnit 来实现? 如果你能实现它,请告诉我如何实现。
我想用下面的PHPUnit实现测试。
如果您能回答是否可能,我会很高兴。
模块名称块.php
/**
* @file
* create block
*/
namespace Drupal\moduleName\Plugin\Block;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Url;
/**
*
* Provides a 'testBlock' block.
* @Block(
* id = "test_block",
* admin_label = @Translation("Test"),
* category = @Translation("Menu"),
* )
*/
class moduleNameBlock extends BlockBase {
/**
* {@inheritdoc}
*/
public function build()
{
$build = [];
$url = '';
$nid = '';
$nid = $this->getCurrentUserNode();
if ( !empty($nid) ) {
$url = Url::fromRoute('entity.node.canonical', ['node' => $nid]);
}
$block = [
'#theme' => 'block_theme',
'#url' => $url,
'#nid' => $nid,
'#cache' => [
'max-age' => 0
]
];
$build['test_block'] = $block;
return $build;
}
/**
* The node associated with the user
* @return nid
*/
private function getCurrentUserNode() {
$user_id = \Drupal\user\Entity\User::load(\Drupal::currentUser()->id());
$nid = $user_id->get('field_name')->getValue();
return $nid[0]['target_id'];
}
}
【问题讨论】: