Skip to content

Commit

Permalink
Update Context to 7.x-3.12
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonathan Hunt committed Feb 11, 2023
1 parent 9014468 commit 11b2b62
Show file tree
Hide file tree
Showing 8 changed files with 138 additions and 15 deletions.
6 changes: 3 additions & 3 deletions docroot/sites/all/modules/contrib/context/context.info
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ files[] = tests/context.test
files[] = tests/context.conditions.test
files[] = tests/context.reactions.test

; Information added by Drupal.org packaging script on 2022-07-26
version = "7.x-3.11"
; Information added by Drupal.org packaging script on 2023-02-09
version = "7.x-3.12"
core = "7.x"
project = "context"
datestamp = "1658874334"
datestamp = "1675905575"
2 changes: 1 addition & 1 deletion docroot/sites/all/modules/contrib/context/context.module
Original file line number Diff line number Diff line change
Expand Up @@ -527,7 +527,7 @@ function context_empty($element) {
/**
* Get a plugin handler.
*/
function context_get_plugin($type = 'condition', $key, $reset = FALSE) {
function context_get_plugin($type, $key, $reset = FALSE) {
static $cache = array();
if (!isset($cache[$type][$key]) || $reset) {
switch ($type) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ core = 7.x

files[] = plugins/context_layouts_reaction_block.inc

; Information added by Drupal.org packaging script on 2022-07-26
version = "7.x-3.11"
; Information added by Drupal.org packaging script on 2023-02-09
version = "7.x-3.12"
core = "7.x"
project = "context"
datestamp = "1658874334"
datestamp = "1675905575"
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ configure = admin/structure/context
files[] = context.module
files[] = tests/context_ui.test

; Information added by Drupal.org packaging script on 2022-07-26
version = "7.x-3.11"
; Information added by Drupal.org packaging script on 2023-02-09
version = "7.x-3.12"
core = "7.x"
project = "context"
datestamp = "1658874334"
datestamp = "1675905575"
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,18 @@ class context_condition_context_all extends context_condition_path {
// and have values set.
if (!in_array($context->name, $active_contexts, TRUE) && $values = $this->fetch_from_context($context, 'values')) {

$contexts_matched = 0;

// Count the matched contexts
foreach($values as $value) {
// Always check against the active contexts.
if ($this->match(array_keys(context_active_contexts()), array($value))) {
$contexts_matched++;
}
}

// The condition is met if all contexts are active.
if (count(array_intersect($values, $active_contexts)) == count($values)) {
if ($contexts_matched == count($values)) {
$this->condition_met($context);
}
}
Expand All @@ -26,4 +36,46 @@ class context_condition_context_all extends context_condition_path {
}
}
}

/**
* Retrieve all context conditions.
*
* This method is slightly adapted to context_condition::get_contexts() in
* order to ensure that a context that is used as condition in another context
* gets handled before.
*/
function get_contexts($value = NULL) {
$map = context_condition_map();
$map = isset($map[$this->plugin]) ? $map[$this->plugin] : array();

$contexts = array();

// Add the contexts that are needed for conditions in the other contexts
// first. Start with the negated ones first, as we can not unset a met
// condition afterwards.
krsort($map);
foreach ($map as $key => $submap) {
// Negated context conditions start with a "~".
if (substr($key, 0, 1) == "~") {
$key = substr($key, 1);
}
if (!isset($contexts[$key])) {
$context = context_load($key);
// Check if context exists. This will fail for wildcards.
if ($context) {
$contexts[$context->name] = $context;
}
}
}
foreach ($map as $key => $submap) {
foreach ($submap as $name) {
if (!isset($contexts[$name])) {
$context = context_load($name);
$contexts[$context->name] = $context;
}
}
}

return $contexts;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,45 @@ class context_condition_user extends context_condition {
return $values;
}

function options_form($context) {
$defaults = $this->fetch_from_context($context, 'options');
return array(
'negate_role' => array(
'#title' => t('Make role a negative condition'),
'#type' => 'checkbox',
'#description' => t("Checking this box will make this condition fire if the user's role is NOT one of the role's checked"),
'#default_value' => isset($defaults['negate_role']) ? $defaults['negate_role'] : 0,
),
);
}

function execute($account) {
$roles = $account->roles;
foreach ($roles as $rid => $role) {
$all_roles = user_roles();
$users_roles = $account->roles;
foreach ($all_roles as $rid => $role) {
foreach ($this->get_contexts($role) as $context) {
$this->condition_met($context, $role);
$options = $this->fetch_from_context($context, 'options');
if (empty($options['negate_role'])) {
if (in_array($role, $users_roles)){
$this->condition_met($context, $role);
}
}
else {
$negate_flag = TRUE;
foreach ($this->fetch_from_context($context, 'values') as $nid => $negated_role) {
if (!in_array($negated_role, $users_roles)) {
$negate_flag &= TRUE;
}
else {
$negate_flag &= FALSE;
}
}
if ($negate_flag) {
$this->condition_met($context, $role);
}
}
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,7 @@ DrupalContextBlockEditor.prototype = {
$('a.context_ui_dialog-stop').hide();

$('.editing-context-label').remove();
var label = $('#context-editable-trigger-'+context+' .label').text();
var label = $('#context-editable-trigger-'+context+' .label.top').text();
label = Drupal.t('Now editing: @label', {'@label': label});
editor.parent().parent().prepend('<div class="editing-context-label">' + label + '</div>');

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,44 @@ class ContextConditionUserTest extends DrupalWebTestCase {
}
}

class ContextConditionNagateUserTest extends DrupalWebTestCase {
protected $profile = 'testing';

public static function getInfo() {
return array(
'name' => 'Condition: nagate user',
'description' => 'Test nagate user condition.',
'group' => 'Context',
);
}

function setUp() {
parent::setUp('context', 'ctools');
$this->user1 = $this->drupalCreateUser(array('access content', 'administer site configuration'));

// Create test context.
ctools_include('export');
$this->context = ctools_export_new_object('context');
$this->context->name = 'testcontext';
$this->context->conditions = array('user' => array('values' => array('authenticated user' => 'authenticated user'), 'options' => array('negate_role' => 1)));
$this->context->reactions = array('debug' => array('debug' => TRUE));
$saved = context_save($this->context);
$this->assertTrue($saved, "Context 'testcontext' saved.");
}

function test() {
// User 1 does not trigger the context.
$this->drupalLogin($this->user1);
$this->drupalGet('node');
$this->assertNoText('Active context: testcontext');

// Anonymous triggers the context.
$this->drupalLogout();
$this->drupalGet('');
$this->assertText('Active context: testcontext');
}
}

class ContextConditionUserPageTest extends DrupalWebTestCase {
protected $profile = 'testing';

Expand Down

0 comments on commit 11b2b62

Please sign in to comment.