Commit a015229d authored by alexpott's avatar alexpott

Issue #1921546 by larowlan, EclipseGc: Create a PHP Filter Condition.

parent 87434066
<?php
/**
* @file
* Contains \Drupal\php\Plugin\Core\Condition\Php.
*/
namespace Drupal\php\Plugin\Core\Condition;
use Drupal\Core\Condition\ConditionPluginBase;
use Drupal\Component\Annotation\Plugin;
use Drupal\Core\Annotation\Translation;
/**
* Provides a 'Php' condition.
*
* @Plugin(
* id = "php",
* label = @Translation("PHP"),
* module = "php"
* )
*/
class Php extends ConditionPluginBase {
/**
* {@inheritdoc}
*/
public function buildForm(array $form, array &$form_state) {
$form = parent::buildForm($form, $form_state);
if (empty($this->configuration['php'])) {
// Initialize an empty value.
$this->configuration['php'] = FALSE;
}
$form['php'] = array(
'#type' => 'textarea',
'#title' => t('When the following PHP return TRUE (experts only)'),
'#default_value' => $this->configuration['php'],
'#description' => t('Enter PHP code between <?php ?>. Note that executing incorrect PHP code can break your Drupal site. Return TRUE in order for this condition to evaluate as TRUE.'),
'#access' => user_access('use PHP for settings')
);
return $form;
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, array &$form_state) {
$this->configuration['php'] = $form_state['values']['php'];
parent::submitForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function summary() {
if (!empty($this->configuration['php'])) {
return t('When the given PHP evaluates as @state.',
array(
'@state' => !empty($this->configuration['negate']) ? 'FALSE' : 'TRUE'
)
);
}
else {
return t('No PHP code has been provided.');
}
}
/**
* {@inheritdoc}
*/
public function evaluate() {
return php_eval($this->configuration['php']);
}
}
<?php
/**
* @file
* Contains \Drupal\php\Tests\Condition\PhpConditionTest.
*/
namespace Drupal\php\Tests\Condition;
use Drupal\simpletest\DrupalUnitTestBase;
/**
* Tests the php condition.
*/
class PhpConditionTest extends DrupalUnitTestBase {
/**
* The condition plugin manager.
*
* @var \Drupal\Core\Condition\ConditionManager
*/
protected $manager;
/**
* Modules to enable.
*
* @var array
*/
public static $modules = array('system', 'php');
public static function getInfo() {
return array(
'name' => 'PHP Condition Plugin',
'description' => 'Tests that the PHP Condition, provided by the php module, is working properly.',
'group' => 'Condition API',
);
}
protected function setUp() {
parent::setUp();
$this->manager = $this->container->get('plugin.manager.condition');
}
/**
* Tests conditions.
*/
public function testConditions() {
// Grab the PHP condition and configure it to check against a php snippet.
$condition = $this->manager->createInstance('php')
->setConfig('php', '<?php return TRUE; ?>');
$this->assertTrue($condition->execute(), 'PHP condition passes as expected.');
// Check for the proper summary.
$this->assertEqual($condition->summary(), 'When the given PHP evaluates as TRUE.');
// Set the PHP snippet to return FALSE.
$condition->setConfig('php', '<?php return FALSE; ?>');
$this->assertFalse($condition->execute(), 'PHP condition fails as expected.');
// Negate the condition.
$condition->setConfig('negate', TRUE);
// Check for the proper summary.
$this->assertEqual($condition->summary(), 'When the given PHP evaluates as FALSE.');
// Reverse the negation.
$condition->setConfig('negate', FALSE);
// Set and empty snippet.
$condition->setConfig('php', FALSE);
// Check for the proper summary.
$this->assertEqual($condition->summary(), 'No PHP code has been provided.');
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment