context_entity_field.test in Context entity field 7
Test context condition.
File
tests/context_entity_field.testView source
<?php
/**
* @file
* Test context condition.
*/
/**
* Basic test definition.
*/
abstract class ContextConditionContextEntityFieldTest extends DrupalWebTestCase {
// We need default content type and taxonomy created.
protected $profile = 'standard';
/**
* {@inerithDocs}
*/
public function setUp() {
parent::setUp('context_entity_field');
}
/**
* Helper method to create contexts.
*
* @param string $name
* Machine name of context to create.
*
* @param array $options
* Settings (condiction) to create context.
*/
protected function contextCreate($name, $options) {
$entities = array();
foreach ($options['entity_type'] as $entity_type) {
$entities[$entity_type] = $entity_type;
}
$options['entity_type'] = serialize($entities);
$condictions = array(
'entity_field' => array(
'values' => array(
'entity_type' => $options['entity_type'],
'field_name' => $options['field_name'],
'field_status' => $options['field_status'],
'field_value' => $options['field_value'],
),
),
);
ctools_include('export');
$context = ctools_export_new_object('context');
$context->name = $name;
$context->conditions = $condictions;
$context->reactions = array(
'debug' => array(
'debug' => TRUE,
),
);
$saved = context_save($context);
$this
->assertTrue($saved, "Context '{$name}' saved.");
}
/**
* Helper method to delete context.
*
* @param string $name
* Machine name of context to delete
*/
protected function contextDelete($name) {
$context = context_load($name);
context_delete($context);
}
}
/**
* Test context contition using text field.
*/
class ContextConditionContextEntityTextFieldTest extends ContextConditionContextEntityFieldTest {
/**
* Nodes generated to be used in tests.
*/
protected $nodes = array();
/**
* Testsuite information.
*
* @return array
* Array with infromation about test suite.
*/
public static function getInfo() {
return array(
'name' => 'Condition: field text',
'description' => 'Test Entity Field condition for Text fields.',
'group' => 'Context Entity Field',
);
}
/**
* Create required data to run tests.
*/
public function setUp() {
parent::setUp();
$this
->createNodes();
$this
->createContexts();
}
/**
* Remove date created to run tests.
*/
public function tearDown() {
parent::tearDown();
// Delete nodes.
node_delete($this->nodes['body_empty']->nid);
node_delete($this->nodes['body_test']->nid);
node_delete($this->nodes['body_other']->nid);
// Delete contexts.
$this
->contextDelete('entity_text_field_empty');
$this
->contextDelete('entity_text_field_all');
$this
->contextDelete('entity_text_field_match');
}
/**
* Create context to test conditions.
*/
protected function createContexts() {
$this
->contextCreate('entity_text_field_empty', array(
'entity_type' => array(
'node',
),
'field_name' => 'body',
'field_status' => 'empty',
'field_value' => '',
));
$this
->contextCreate('entity_text_field_all', array(
'entity_type' => array(
'node',
),
'field_name' => 'body',
'field_status' => 'all',
'field_value' => 'other',
));
$this
->contextCreate('entity_text_field_match', array(
'entity_type' => array(
'node',
),
'field_name' => 'body',
'field_status' => 'match',
'field_value' => 'test',
));
}
/**
* Create nodes to test context condictions.
*/
protected function createNodes() {
$settings = array(
'type' => 'article',
'title' => 'Article body: EMPTY',
);
$this->nodes['body_empty'] = $this
->drupalCreateNode($settings);
$this->nodes['body_empty']->body[LANGUAGE_NONE] = array();
node_save($this->nodes['body_empty']);
$settings = array(
'type' => 'article',
'title' => 'Article body: EMPTY',
'body' => array(
LANGUAGE_NONE => array(
array(
'value' => 'test',
),
),
),
);
$this->nodes['body_test'] = $this
->drupalCreateNode($settings);
$settings = array(
'type' => 'article',
'title' => 'Article body: other',
'body' => array(
LANGUAGE_NONE => array(
array(
'value' => 'other',
),
),
),
);
$this->nodes['body_other'] = $this
->drupalCreateNode($settings);
}
/**
* Check context for 'empty' setting.
*/
public function testTextSingleEmptyField() {
$this
->drupalGet("node/" . $this->nodes['body_empty']->nid);
$this
->assertText("Active context: entity_text_field_empty");
$this
->drupalGet("node/" . $this->nodes['body_test']->nid);
$this
->assertNoText("Active context: entity_text_field_empty");
$this
->drupalGet("node/" . $this->nodes['body_other']->nid);
$this
->assertNoText("Active context: entity_text_field_empty");
}
/**
* Check condition for 'all' setting.
*/
public function testTextSingleAllField() {
$this
->drupalGet("node/" . $this->nodes['body_empty']->nid);
$this
->assertNoText("Active context: entity_text_field_all");
$this
->drupalGet("node/" . $this->nodes['body_test']->nid);
$this
->assertText("Active context: entity_text_field_all");
$this
->drupalGet("node/" . $this->nodes['body_other']->nid);
$this
->assertText("Active context: entity_text_field_all");
}
/**
* Check condition for 'match' setting.
*/
public function testTextSingleMatchField() {
$this
->drupalGet("node/" . $this->nodes['body_empty']->nid);
$this
->assertNoText("Active context: entity_text_field_match");
$this
->drupalGet("node/" . $this->nodes['body_test']->nid);
$this
->assertText("Active context: entity_text_field_match");
$this
->drupalGet("node/" . $this->nodes['body_other']->nid);
$this
->assertNoText("Active context: entity_text_field_match");
}
}
/**
* Test context contition using taxonomy field.
*/
class ContextConditionContextEntityTaxonomyFieldTest extends ContextConditionContextEntityFieldTest {
/**
* Nodes generated to be used in tests.
*/
protected $nodes = array();
/**
* Terms generated to be used in tests.
*/
protected $terms = array();
/**
* Testsuite information.
*
* @return array
* Array with infromation about test suite.
*/
public static function getInfo() {
return array(
'name' => 'Condition: field taxonomy',
'description' => 'Test Entity Field condition for Taxonomy fields.',
'group' => 'Context Entity Field',
);
}
/**
* Create required data to run tests.
*/
public function setUp() {
parent::setUp();
$this
->createTerms();
$this
->createNodes();
$this
->createContexts();
}
/**
* Remove date created to run tests.
*/
public function tearDown() {
parent::tearDown();
// Delete nodes.
node_delete($this->nodes['terms_empty']->nid);
node_delete($this->nodes['terms_apples']->nid);
node_delete($this->nodes['terms_oranges']->nid);
node_delete($this->nodes['terms_all']->nid);
// Delete terms.
taxonomy_term_delete($this->terms['apples']->tid);
taxonomy_term_delete($this->terms['oranges']->tid);
// Delete contexts.
$this
->contextDelete('entity_term_field_empty');
$this
->contextDelete('entity_term_field_all');
$this
->contextDelete('entity_term_field_match');
}
/**
* Create terms to test context condictions.
*/
protected function createTerms() {
$this->vocab = taxonomy_vocabulary_machine_name_load('tags');
$this->terms = array();
$this->terms['apples'] = (object) array(
'name' => 'apples',
'vid' => $this->vocab->vid,
);
$this->terms['oranges'] = (object) array(
'name' => 'oranges',
'vid' => $this->vocab->vid,
);
taxonomy_term_save($this->terms['apples']);
taxonomy_term_save($this->terms['oranges']);
}
/**
* Create nodes to test context condictions.
*/
protected function createNodes() {
$settings = array(
'type' => 'article',
'title' => 'Article terms: EMPTY',
);
$this->nodes['terms_empty'] = $this
->drupalCreateNode($settings);
$settings = array(
'type' => 'article',
'title' => 'Article term: oranges',
'field_tags' => array(
LANGUAGE_NONE => array(
array(
'tid' => $this->terms['oranges']->tid,
),
),
),
);
$this->nodes['terms_oranges'] = $this
->drupalCreateNode($settings);
$settings = array(
'type' => 'article',
'title' => 'Article term: apples',
'field_tags' => array(
LANGUAGE_NONE => array(
array(
'tid' => $this->terms['apples']->tid,
),
),
),
);
$this->nodes['terms_apples'] = $this
->drupalCreateNode($settings);
$settings = array(
'type' => 'article',
'title' => 'Article terms: oranges & apples',
'field_tags' => array(
LANGUAGE_NONE => array(
array(
'tid' => $this->terms['apples']->tid,
),
array(
'tid' => $this->terms['oranges']->tid,
),
),
),
);
$this->nodes['terms_all'] = $this
->drupalCreateNode($settings);
}
/**
* Create context to test conditions.
*/
protected function createContexts() {
$this
->contextCreate('entity_term_field_empty', array(
'entity_type' => array(
'node',
),
'field_name' => 'field_tags',
'field_status' => 'empty',
'field_value' => $this->terms['oranges']->tid,
));
$this
->contextCreate('entity_term_field_all', array(
'entity_type' => array(
'node',
),
'field_name' => 'field_tags',
'field_status' => 'all',
'field_value' => $this->terms['oranges']->tid,
));
$this
->contextCreate('entity_term_field_match', array(
'entity_type' => array(
'node',
),
'field_name' => 'field_tags',
'field_status' => 'match',
'field_value' => $this->terms['oranges']->tid,
));
}
/**
* Check condition for 'empty' setting.
*/
public function testTermSingleEmptyField() {
$this
->drupalGet("node/" . $this->nodes['terms_empty']->nid);
$this
->assertText("Active context: entity_term_field_empty");
$this
->drupalGet("node/" . $this->nodes['terms_apples']->nid);
$this
->assertNoText("Active context: entity_term_field_empty");
$this
->drupalGet("node/" . $this->nodes['terms_oranges']->nid);
$this
->assertNoText("Active context: entity_term_field_empty");
$this
->drupalGet("node/" . $this->nodes['terms_all']->nid);
$this
->assertNoText("Active context: entity_term_field_empty");
}
/**
* Check condition for 'all' setting.
*/
public function testTermSingleAllField() {
$this
->drupalGet("node/" . $this->nodes['terms_empty']->nid);
$this
->assertNoText("Active context: entity_term_field_all");
$this
->drupalGet("node/" . $this->nodes['terms_apples']->nid);
$this
->assertText("Active context: entity_term_field_all");
$this
->drupalGet("node/" . $this->nodes['terms_oranges']->nid);
$this
->assertText("Active context: entity_term_field_all");
$this
->drupalGet("node/" . $this->nodes['terms_all']->nid);
$this
->assertText("Active context: entity_term_field_all");
}
/**
* Check condition for 'match' setting.
*/
public function testTermSingleMatchField() {
$this
->drupalGet("node/" . $this->nodes['terms_empty']->nid);
$this
->assertNoText("Active context: entity_term_field_match");
$this
->drupalGet("node/" . $this->nodes['terms_apples']->nid);
$this
->assertNoText("Active context: entity_term_field_match");
$this
->drupalGet("node/" . $this->nodes['terms_oranges']->nid);
$this
->assertText("Active context: entity_term_field_match");
$this
->drupalGet("node/" . $this->nodes['terms_all']->nid);
$this
->assertText("Active context: entity_term_field_match");
}
}
/**
* Test context contition to entity without field.
*/
class ContextConditionContextEntityNoFieldTest extends ContextConditionContextEntityFieldTest {
/**
* Nodes generated to be used in tests.
*/
protected $nodes = array();
/**
* Testsuite information.
*
* @return array
* Array with infromation about test suite.
*/
public static function getInfo() {
return array(
'name' => 'Condition: field missing',
'description' => 'Test Entity Field condition for missing fields.',
'group' => 'Context Entity Field',
);
}
/**
* Create required data to run tests.
*/
public function setUp() {
parent::setUp();
$this
->createNodes();
$this
->createContexts();
}
/**
* Remove date created to run tests.
*/
public function tearDown() {
parent::tearDown();
// Delete nodes.
node_delete($this->nodes['body_empty']->nid);
node_delete($this->nodes['body_test']->nid);
node_delete($this->nodes['body_other']->nid);
// Delete contexts.
$this
->contextDelete('entity_text_field_empty');
$this
->contextDelete('entity_text_field_all');
$this
->contextDelete('entity_text_field_match');
}
/**
* Create context to test conditions.
*/
protected function createContexts() {
$this
->contextCreate('entity_missing_field_empty', array(
'entity_type' => array(
'node',
),
'field_name' => 'missing',
'field_status' => 'empty',
'field_value' => '',
));
$this
->contextCreate('entity_missing_field_all', array(
'entity_type' => array(
'node',
),
'field_name' => 'missing',
'field_status' => 'all',
'field_value' => 'other',
));
$this
->contextCreate('entity_missing_field_match', array(
'entity_type' => array(
'node',
),
'field_name' => 'missing',
'field_status' => 'match',
'field_value' => 'test',
));
}
/**
* Create nodes to test context condictions.
*/
protected function createNodes() {
$settings = array(
'type' => 'article',
'title' => 'Article body: EMPTY',
);
$this->nodes['body_empty'] = $this
->drupalCreateNode($settings);
$this->nodes['body_empty']->body[LANGUAGE_NONE] = array();
node_save($this->nodes['body_empty']);
$settings = array(
'type' => 'article',
'title' => 'Article body: EMPTY',
'body' => array(
LANGUAGE_NONE => array(
array(
'value' => 'test',
),
),
),
);
$this->nodes['body_test'] = $this
->drupalCreateNode($settings);
$settings = array(
'type' => 'article',
'title' => 'Article body: other',
'body' => array(
LANGUAGE_NONE => array(
array(
'value' => 'other',
),
),
),
);
$this->nodes['body_other'] = $this
->drupalCreateNode($settings);
}
/**
* Check context for 'empty' setting.
*/
public function testTextSingleEmptyField() {
$this
->drupalGet("node/" . $this->nodes['body_empty']->nid);
$this
->assertNoText("Active context: entity_missing_field_empty");
$this
->drupalGet("node/" . $this->nodes['body_test']->nid);
$this
->assertNoText("Active context: entity_missing_field_empty");
$this
->drupalGet("node/" . $this->nodes['body_other']->nid);
$this
->assertNoText("Active context: entity_missing_field_empty");
}
/**
* Check condition for 'all' setting.
*/
public function testTextSingleAllField() {
$this
->drupalGet("node/" . $this->nodes['body_empty']->nid);
$this
->assertNoText("Active context: entity_missing_field_all");
$this
->drupalGet("node/" . $this->nodes['body_test']->nid);
$this
->assertNoText("Active context: entity_missing_field_all");
$this
->drupalGet("node/" . $this->nodes['body_other']->nid);
$this
->assertNoText("Active context: entity_missing_field_all");
}
/**
* Check condition for 'match' setting.
*/
public function testTextSingleMatchField() {
$this
->drupalGet("node/" . $this->nodes['body_empty']->nid);
$this
->assertNoText("Active context: entity_missing_field_match");
$this
->drupalGet("node/" . $this->nodes['body_test']->nid);
$this
->assertNoText("Active context: entity_missing_field_match");
$this
->drupalGet("node/" . $this->nodes['body_other']->nid);
$this
->assertNoText("Active context: entity_missing_field_match");
}
}
Classes
Name | Description |
---|---|
ContextConditionContextEntityFieldTest | Basic test definition. |
ContextConditionContextEntityNoFieldTest | Test context contition to entity without field. |
ContextConditionContextEntityTaxonomyFieldTest | Test context contition using taxonomy field. |
ContextConditionContextEntityTextFieldTest | Test context contition using text field. |