commit c2ac6d1dc29ecf16092baded14bd1ad254f69f1f
Author: Assos Assos <assos+forge@centrale-marseille.fr>
Date:   Wed Feb 13 23:43:08 2019 +0100

    Weekly update of contrib modules

diff --git a/drupal7/sites/all/modules/ctools/bulk_export/bulk_export.info b/drupal7/sites/all/modules/ctools/bulk_export/bulk_export.info
index 388cd6f7..9bb33944 100644
--- a/drupal7/sites/all/modules/ctools/bulk_export/bulk_export.info
+++ b/drupal7/sites/all/modules/ctools/bulk_export/bulk_export.info
@@ -3,11 +3,9 @@ description = Performs bulk exporting of data objects known about by Chaos tools
 core = 7.x
 dependencies[] = ctools
 package = Chaos tool suite
-version = CTOOLS_MODULE_VERSION
 
-; Information added by Drupal.org packaging script on 2018-02-24
-version = "7.x-1.14"
+; Information added by Drupal.org packaging script on 2019-02-08
+version = "7.x-1.15"
 core = "7.x"
 project = "ctools"
-datestamp = "1519455788"
-
+datestamp = "1549603691"
diff --git a/drupal7/sites/all/modules/ctools/css/dropbutton.css b/drupal7/sites/all/modules/ctools/css/dropbutton.css
index 376fc674..486ac3ed 100644
--- a/drupal7/sites/all/modules/ctools/css/dropbutton.css
+++ b/drupal7/sites/all/modules/ctools/css/dropbutton.css
@@ -19,7 +19,6 @@
   height: auto;
   position: absolute;
   right: 0;
-  text-indent: -9999px; /* LTR */
   top: 0;
   width: 17px;
 }
@@ -50,6 +49,7 @@
   right: 6px;
   position: absolute;
   top: 0.75em;
+  white-space: nowrap;
 }
 
 .ctools-dropbutton-processed.open .ctools-twisty {
diff --git a/drupal7/sites/all/modules/ctools/ctools.info b/drupal7/sites/all/modules/ctools/ctools.info
index c9d0c63a..188b55dd 100644
--- a/drupal7/sites/all/modules/ctools/ctools.info
+++ b/drupal7/sites/all/modules/ctools/ctools.info
@@ -19,9 +19,8 @@ files[] = tests/object_cache.test
 files[] = tests/object_cache_unit.test
 files[] = tests/page_tokens.test
 
-; Information added by Drupal.org packaging script on 2018-02-24
-version = "7.x-1.14"
+; Information added by Drupal.org packaging script on 2019-02-08
+version = "7.x-1.15"
 core = "7.x"
 project = "ctools"
-datestamp = "1519455788"
-
+datestamp = "1549603691"
diff --git a/drupal7/sites/all/modules/ctools/ctools.module b/drupal7/sites/all/modules/ctools/ctools.module
index 3a805808..62f29192 100644
--- a/drupal7/sites/all/modules/ctools/ctools.module
+++ b/drupal7/sites/all/modules/ctools/ctools.module
@@ -22,6 +22,9 @@ define('CTOOLS_API_VERSION', '2.0.9');
  * simply include a dependency line in that module's info file, e.g.:
  *   ; Requires CTools v7.x-1.4 or newer.
  *   dependencies[] = ctools (>=1.4)
+ *
+ * @deprecated in CTools 1.15 and will be removed before CTools 2.0.0.
+ *   Use the version provided by the drupal.org packaging system.
  */
 define('CTOOLS_MODULE_VERSION', '7.x-1.13');
 
diff --git a/drupal7/sites/all/modules/ctools/ctools_access_ruleset/ctools_access_ruleset.info b/drupal7/sites/all/modules/ctools/ctools_access_ruleset/ctools_access_ruleset.info
index 84a922fb..3fc35564 100644
--- a/drupal7/sites/all/modules/ctools/ctools_access_ruleset/ctools_access_ruleset.info
+++ b/drupal7/sites/all/modules/ctools/ctools_access_ruleset/ctools_access_ruleset.info
@@ -2,12 +2,10 @@ name = Custom rulesets
 description = Create custom, exportable, reusable access rulesets for applications like Panels.
 core = 7.x
 package = Chaos tool suite
-version = CTOOLS_MODULE_VERSION
 dependencies[] = ctools
 
-; Information added by Drupal.org packaging script on 2018-02-24
-version = "7.x-1.14"
+; Information added by Drupal.org packaging script on 2019-02-08
+version = "7.x-1.15"
 core = "7.x"
 project = "ctools"
-datestamp = "1519455788"
-
+datestamp = "1549603691"
diff --git a/drupal7/sites/all/modules/ctools/ctools_ajax_sample/ctools_ajax_sample.info b/drupal7/sites/all/modules/ctools/ctools_ajax_sample/ctools_ajax_sample.info
index 4bb8943c..c2a52df4 100644
--- a/drupal7/sites/all/modules/ctools/ctools_ajax_sample/ctools_ajax_sample.info
+++ b/drupal7/sites/all/modules/ctools/ctools_ajax_sample/ctools_ajax_sample.info
@@ -1,13 +1,11 @@
 name = Chaos Tools (CTools) AJAX Example
 description = Shows how to use the power of Chaos AJAX.
 package = Chaos tool suite
-version = CTOOLS_MODULE_VERSION
 dependencies[] = ctools
 core = 7.x
 
-; Information added by Drupal.org packaging script on 2018-02-24
-version = "7.x-1.14"
+; Information added by Drupal.org packaging script on 2019-02-08
+version = "7.x-1.15"
 core = "7.x"
 project = "ctools"
-datestamp = "1519455788"
-
+datestamp = "1549603691"
diff --git a/drupal7/sites/all/modules/ctools/ctools_custom_content/ctools_custom_content.info b/drupal7/sites/all/modules/ctools/ctools_custom_content/ctools_custom_content.info
index 637bf25d..2bc73489 100644
--- a/drupal7/sites/all/modules/ctools/ctools_custom_content/ctools_custom_content.info
+++ b/drupal7/sites/all/modules/ctools/ctools_custom_content/ctools_custom_content.info
@@ -2,12 +2,10 @@ name = Custom content panes
 description = Create custom, exportable, reusable content panes for applications like Panels.
 core = 7.x
 package = Chaos tool suite
-version = CTOOLS_MODULE_VERSION
 dependencies[] = ctools
 
-; Information added by Drupal.org packaging script on 2018-02-24
-version = "7.x-1.14"
+; Information added by Drupal.org packaging script on 2019-02-08
+version = "7.x-1.15"
 core = "7.x"
 project = "ctools"
-datestamp = "1519455788"
-
+datestamp = "1549603691"
diff --git a/drupal7/sites/all/modules/ctools/ctools_plugin_example/ctools_plugin_example.info b/drupal7/sites/all/modules/ctools/ctools_plugin_example/ctools_plugin_example.info
index 40f133bc..3f268dbc 100644
--- a/drupal7/sites/all/modules/ctools/ctools_plugin_example/ctools_plugin_example.info
+++ b/drupal7/sites/all/modules/ctools/ctools_plugin_example/ctools_plugin_example.info
@@ -1,16 +1,14 @@
 name = Chaos Tools (CTools) Plugin Example
 description = Shows how an external module can provide ctools plugins (for Panels, etc.).
 package = Chaos tool suite
-version = CTOOLS_MODULE_VERSION
 dependencies[] = ctools
 dependencies[] = panels
 dependencies[] = page_manager
 dependencies[] = advanced_help
 core = 7.x
 
-; Information added by Drupal.org packaging script on 2018-02-24
-version = "7.x-1.14"
+; Information added by Drupal.org packaging script on 2019-02-08
+version = "7.x-1.15"
 core = "7.x"
 project = "ctools"
-datestamp = "1519455788"
-
+datestamp = "1549603691"
diff --git a/drupal7/sites/all/modules/ctools/ctools_plugin_example/ctools_plugin_example.pages_default.inc b/drupal7/sites/all/modules/ctools/ctools_plugin_example/ctools_plugin_example.pages_default.inc
index bbabf227..ed3ffd77 100644
--- a/drupal7/sites/all/modules/ctools/ctools_plugin_example/ctools_plugin_example.pages_default.inc
+++ b/drupal7/sites/all/modules/ctools/ctools_plugin_example/ctools_plugin_example.pages_default.inc
@@ -95,6 +95,7 @@ function ctools_plugin_example_default_page_manager_pages() {
     'access' => array(
       'logic' => 'and',
     ),
+    'pipeline' => 'standard',
   );
   $display = new panels_display();
   $display->layout = 'threecol_33_34_33_stacked';
@@ -154,7 +155,7 @@ function ctools_plugin_example_default_page_manager_pages() {
   $pane->configuration = array(
     'title' => 'Long Arg Visibility Block',
     'body' => 'This block will be here when the argument is longer than configured arg length. It uses the \'arg_length\' access plugin to test against the length of the argument used for Simplecontext.',
-    'format' => '1',
+    'format' => 'filtered_html',
     'substitute' => 1,
   );
   $pane->cache = array();
@@ -185,7 +186,7 @@ function ctools_plugin_example_default_page_manager_pages() {
   $pane->configuration = array(
     'title' => 'Short Arg Visibility',
     'body' => 'This block appears when the simplecontext argument is <i>less than</i> the configured length.',
-    'format' => '1',
+    'format' => 'filtered_html',
     'substitute' => 1,
   );
   $pane->cache = array();
@@ -297,7 +298,7 @@ function ctools_plugin_example_default_page_manager_pages() {
     item1 is %sc:item1
     item2 is %sc:item2
     description is %sc:description',
-    'format' => '1',
+    'format' => 'filtered_html',
     'substitute' => 1,
   );
   $pane->cache = array();
@@ -363,7 +364,7 @@ function ctools_plugin_example_default_page_manager_pages() {
     'body' => 'The CTools Plugin Example module (and this panel page) are just here to demonstrate how to build CTools plugins.
 
             ',
-    'format' => '2',
+    'format' => 'full_html',
     'substitute' => 1,
   );
   $pane->cache = array();
@@ -408,6 +409,7 @@ function ctools_plugin_example_default_page_manager_pages() {
     'css' => '',
     'contexts' => array(),
     'relationships' => array(),
+    'pipeline' => 'standard',
   );
   $display = new panels_display();
   $display->layout = 'onecol';
@@ -428,7 +430,7 @@ function ctools_plugin_example_default_page_manager_pages() {
   $pane->configuration = array(
     'title' => 'Use this page with an argument',
     'body' => 'This demo page works if you use an argument, like <a href="ctools_plugin_example/xxxxx">ctools_plugin_example/xxxxx</a>.',
-    'format' => '1',
+    'format' => 'filtered_html',
     'substitute' => NULL,
   );
   $pane->cache = array();
diff --git a/drupal7/sites/all/modules/ctools/drush/ctools.drush.inc b/drupal7/sites/all/modules/ctools/drush/ctools.drush.inc
index 3677b171..34fd4d3a 100644
--- a/drupal7/sites/all/modules/ctools/drush/ctools.drush.inc
+++ b/drupal7/sites/all/modules/ctools/drush/ctools.drush.inc
@@ -452,6 +452,7 @@ function ctools_drush_export_info() {
  * all arg handling etc in one place.
  */
 function ctools_drush_export_op_command() {
+  $args = func_get_args();
   // Get all info for the current drush command.
   $command = drush_get_command();
   $op = '';
@@ -495,7 +496,6 @@ function ctools_drush_export_op_command() {
     }
   }
   else {
-    $args = func_get_args();
     // Table name should always be first arg...
     $table_name = array_shift($args);
     // Any additional args are assumed to be exportable names.
diff --git a/drupal7/sites/all/modules/ctools/includes/dropbutton.theme.inc b/drupal7/sites/all/modules/ctools/includes/dropbutton.theme.inc
index d69c9883..4b95ef0d 100644
--- a/drupal7/sites/all/modules/ctools/includes/dropbutton.theme.inc
+++ b/drupal7/sites/all/modules/ctools/includes/dropbutton.theme.inc
@@ -104,10 +104,10 @@ function theme_links__ctools_dropbutton($vars) {
 
       $output .= '<div class="ctools-link">';
       if ($vars['image']) {
-        $output .= '<a href="#" class="ctools-twisty ctools-image">' . $vars['title'] . '</a>';
+        $output .= '<a href="#" class="ctools-twisty ctools-image"><span class="element-invisible">' . $vars['title'] . '</span></a>';
       }
       else {
-        $output .= '<a href="#" class="ctools-twisty ctools-text">' . $vars['title'] . '</a>';
+        $output .= '<a href="#" class="ctools-twisty ctools-text"><span class="element-invisible">' . $vars['title'] . '</span></a>';
       }
       // ctools-link.
       $output .= '</div>';
diff --git a/drupal7/sites/all/modules/ctools/includes/export-ui.inc b/drupal7/sites/all/modules/ctools/includes/export-ui.inc
index 1eb87ae7..72e5a2fd 100644
--- a/drupal7/sites/all/modules/ctools/includes/export-ui.inc
+++ b/drupal7/sites/all/modules/ctools/includes/export-ui.inc
@@ -333,7 +333,7 @@ function ctools_export_ui_process(&$plugin, $info) {
 
   $plugin['strings']['confirmation']['delete'] += array(
     'question' => t('Are you sure you want to delete %title?'),
-    'information' => t('This action will permanently remove this item from your database..'),
+    'information' => t('This action will permanently remove this item from your database.'),
     'success' => t('The item has been deleted.'),
   );
 
diff --git a/drupal7/sites/all/modules/ctools/includes/wizard.inc b/drupal7/sites/all/modules/ctools/includes/wizard.inc
index 8b0d061c..e0b8fbd1 100644
--- a/drupal7/sites/all/modules/ctools/includes/wizard.inc
+++ b/drupal7/sites/all/modules/ctools/includes/wizard.inc
@@ -503,7 +503,7 @@ function ctools_wizard_defaults(&$form_info) {
   $form_info = $form_info + $defaults;
   // Set form callbacks if they aren't defined.
   foreach ($form_info['forms'] as $step => $params) {
-    if (!$params['form id']) {
+    if (empty($params['form id'])) {
       $form_callback = $hook . '_' . $step . '_form';
       $form_info['forms'][$step]['form id'] = $form_callback;
     }
diff --git a/drupal7/sites/all/modules/ctools/js/modal.js b/drupal7/sites/all/modules/ctools/js/modal.js
index e65f51af..ec7b02ae 100644
--- a/drupal7/sites/all/modules/ctools/js/modal.js
+++ b/drupal7/sites/all/modules/ctools/js/modal.js
@@ -378,7 +378,7 @@
       }
     }
 
-    if (!speed) {
+    if (!speed && 0 !== speed) {
       speed = 'fast';
     }
 
@@ -555,7 +555,7 @@
     // Create our content div, get the dimensions, and hide it
     var modalContent = $('#modalContent').css('top','-1000px');
     var $modalHeader = modalContent.find('.modal-header');
-    var mdcTop = wt + ( winHeight / 2 ) - (  modalContent.outerHeight() / 2);
+    var mdcTop = wt + Math.max((winHeight / 2) - (modalContent.outerHeight() / 2), 0);
     var mdcLeft = ( winWidth / 2 ) - ( modalContent.outerWidth() / 2);
     $('#modalBackdrop').css(css).css('top', 0).css('height', docHeight + 'px').css('width', docWidth + 'px').show();
     modalContent.css({top: mdcTop + 'px', left: mdcLeft + 'px'}).hide()[animation](speed);
@@ -588,35 +588,43 @@
       $('body').unbind( 'keypress', modalEventHandler );
       $('body').unbind( 'keydown', modalTabTrapHandler );
       $('.close', $modalHeader).unbind('click', modalContentClose);
-      $('body').unbind('keypress', modalEventEscapeCloseHandler);
+      $(document).unbind('keydown', modalEventEscapeCloseHandler);
       $(document).trigger('CToolsDetachBehaviors', $('#modalContent'));
 
-      // Set our animation parameters and use them
-      if ( animation == 'fadeIn' ) animation = 'fadeOut';
-      if ( animation == 'slideDown' ) animation = 'slideUp';
-      if ( animation == 'show' ) animation = 'hide';
+      // Closing animation.
+      switch (animation) {
+        case 'fadeIn':
+          modalContent.fadeOut(speed, modalContentRemove);
+          break;
 
-      // Close the content
-      modalContent.hide()[animation](speed);
+        case 'slideDown':
+          modalContent.slideUp(speed, modalContentRemove);
+          break;
 
-      // Remove the content
+        case 'show':
+          modalContent.hide(speed, modalContentRemove);
+          break;
+      }
+    }
+
+    // Remove the content.
+    modalContentRemove = function () {
       $('#modalContent').remove();
       $('#modalBackdrop').remove();
 
-      // Restore focus to where it was before opening the dialog
+      // Restore focus to where it was before opening the dialog.
       $(oldFocus).focus();
     };
 
     // Move and resize the modalBackdrop and modalContent on window resize.
-    modalContentResize = function(){
-
+    modalContentResize = function () {
       // Reset the backdrop height/width to get accurate document size.
       $('#modalBackdrop').css('height', '').css('width', '');
 
       // Position code lifted from:
       // http://www.quirksmode.org/viewport/compatibility.html
       if (self.pageYOffset) { // all except Explorer
-      var wt = self.pageYOffset;
+        var wt = self.pageYOffset;
       } else if (document.documentElement && document.documentElement.scrollTop) { // Explorer 6 Strict
         var wt = document.documentElement.scrollTop;
       } else if (document.body) { // all other Explorers
@@ -632,7 +640,7 @@
 
       // Get where we should move content to
       var modalContent = $('#modalContent');
-      var mdcTop = wt + ( winHeight / 2 ) - ( modalContent.outerHeight() / 2);
+      var mdcTop = wt + Math.max((winHeight / 2) - (modalContent.outerHeight() / 2), 0);
       var mdcLeft = ( winWidth / 2 ) - ( modalContent.outerWidth() / 2);
 
       // Apply the changes
diff --git a/drupal7/sites/all/modules/ctools/page_manager/page_manager.info b/drupal7/sites/all/modules/ctools/page_manager/page_manager.info
index 58661385..a56663b8 100644
--- a/drupal7/sites/all/modules/ctools/page_manager/page_manager.info
+++ b/drupal7/sites/all/modules/ctools/page_manager/page_manager.info
@@ -3,13 +3,11 @@ description = Provides a UI and API to manage pages within the site.
 core = 7.x
 dependencies[] = ctools
 package = Chaos tool suite
-version = CTOOLS_MODULE_VERSION
 
 files[] = tests/head_links.test
 
-; Information added by Drupal.org packaging script on 2018-02-24
-version = "7.x-1.14"
+; Information added by Drupal.org packaging script on 2019-02-08
+version = "7.x-1.15"
 core = "7.x"
 project = "ctools"
-datestamp = "1519455788"
-
+datestamp = "1549603691"
diff --git a/drupal7/sites/all/modules/ctools/page_manager/plugins/tasks/page.inc b/drupal7/sites/all/modules/ctools/page_manager/plugins/tasks/page.inc
index 8d24d330..100dfd81 100644
--- a/drupal7/sites/all/modules/ctools/page_manager/plugins/tasks/page.inc
+++ b/drupal7/sites/all/modules/ctools/page_manager/plugins/tasks/page.inc
@@ -261,6 +261,7 @@ function page_manager_page_theme(&$items, $task) {
  *   creating named arguments in the path.
  */
 function page_manager_page_execute($subtask_id) {
+  $func_args = func_get_args();
   $page = page_manager_page_load($subtask_id);
   $task = page_manager_get_task($page->task);
   $subtask = page_manager_get_task_subtask($task, $subtask_id);
@@ -268,7 +269,7 @@ function page_manager_page_execute($subtask_id) {
   // Turn the contexts into a properly keyed array.
   $contexts = array();
   $args = array();
-  foreach (func_get_args() as $count => $arg) {
+  foreach ($func_args as $count => $arg) {
     if (is_object($arg) && get_class($arg) == 'ctools_context') {
       $contexts[$arg->id] = $arg;
       $args[] = $arg->original_argument;
@@ -430,7 +431,7 @@ function page_manager_page_save(&$page) {
 /**
  * Remove a page subtask.
  */
-function page_manager_page_delete($page) {
+function page_manager_page_delete($page, $skip_menu_rebuild = FALSE) {
   $task = page_manager_get_task($page->task);
   if ($function = ctools_plugin_get_function($task, 'delete')) {
     $function($page);
@@ -448,7 +449,11 @@ function page_manager_page_delete($page) {
   // rebuild this page again.
   ctools_include('export');
   ctools_export_load_object_reset('page_manager_pages');
-  menu_rebuild();
+  // Allow menu rebuild to be skipped when calling code is deleting multiple
+  // pages.
+  if (!$skip_menu_rebuild) {
+    menu_rebuild();
+  }
 }
 
 /**
diff --git a/drupal7/sites/all/modules/ctools/plugins/content_types/node_form/node_form_attachments.inc b/drupal7/sites/all/modules/ctools/plugins/content_types/node_form/node_form_attachments.inc
index c55e0752..51c95a5e 100644
--- a/drupal7/sites/all/modules/ctools/plugins/content_types/node_form/node_form_attachments.inc
+++ b/drupal7/sites/all/modules/ctools/plugins/content_types/node_form/node_form_attachments.inc
@@ -21,7 +21,7 @@ if (module_exists('upload')) {
 
 function ctools_node_form_attachments_content_type_render($subtype, $conf, $panel_args, &$context) {
   $block = new stdClass();
-  $block->module = t('node_form');
+  $block->module = 'node_form';
 
   $block->title = t('Attach files');
   $block->delta = 'url-path-options';
diff --git a/drupal7/sites/all/modules/ctools/plugins/content_types/node_form/node_form_author.inc b/drupal7/sites/all/modules/ctools/plugins/content_types/node_form/node_form_author.inc
index f9e93719..86f3d731 100644
--- a/drupal7/sites/all/modules/ctools/plugins/content_types/node_form/node_form_author.inc
+++ b/drupal7/sites/all/modules/ctools/plugins/content_types/node_form/node_form_author.inc
@@ -17,7 +17,7 @@ $plugin = array(
 
 function ctools_node_form_author_content_type_render($subtype, $conf, $panel_args, &$context) {
   $block = new stdClass();
-  $block->module = t('node_form');
+  $block->module = 'node_form';
 
   $block->title = t('Authoring information');
   $block->delta = 'author-options';
diff --git a/drupal7/sites/all/modules/ctools/plugins/content_types/node_form/node_form_book.inc b/drupal7/sites/all/modules/ctools/plugins/content_types/node_form/node_form_book.inc
index af022bfd..b7ad36bd 100644
--- a/drupal7/sites/all/modules/ctools/plugins/content_types/node_form/node_form_book.inc
+++ b/drupal7/sites/all/modules/ctools/plugins/content_types/node_form/node_form_book.inc
@@ -21,7 +21,7 @@ if (module_exists('book')) {
 
 function ctools_node_form_book_content_type_render($subtype, $conf, $panel_args, &$context) {
   $block = new stdClass();
-  $block->module = t('node_form');
+  $block->module = 'node_form';
 
   $block->title = t('Book outline');
   $block->delta = 'book-outline';
diff --git a/drupal7/sites/all/modules/ctools/plugins/content_types/node_form/node_form_buttons.inc b/drupal7/sites/all/modules/ctools/plugins/content_types/node_form/node_form_buttons.inc
index 560a01e4..ed553a67 100644
--- a/drupal7/sites/all/modules/ctools/plugins/content_types/node_form/node_form_buttons.inc
+++ b/drupal7/sites/all/modules/ctools/plugins/content_types/node_form/node_form_buttons.inc
@@ -17,7 +17,7 @@ $plugin = array(
 
 function ctools_node_form_buttons_content_type_render($subtype, $conf, $panel_args, &$context) {
   $block = new stdClass();
-  $block->module = t('node_form');
+  $block->module = 'node_form';
 
   $block->title = '';
   $block->delta = 'buttons';
diff --git a/drupal7/sites/all/modules/ctools/plugins/content_types/node_form/node_form_comment.inc b/drupal7/sites/all/modules/ctools/plugins/content_types/node_form/node_form_comment.inc
index 352681cc..eaf5c954 100644
--- a/drupal7/sites/all/modules/ctools/plugins/content_types/node_form/node_form_comment.inc
+++ b/drupal7/sites/all/modules/ctools/plugins/content_types/node_form/node_form_comment.inc
@@ -21,7 +21,7 @@ if (module_exists('comment')) {
 
 function ctools_node_form_comment_content_type_render($subtype, $conf, $panel_args, &$context) {
   $block = new stdClass();
-  $block->module = t('node_form');
+  $block->module = 'node_form';
 
   $block->title = t('Comment options');
   $block->delta = 'comment-options';
diff --git a/drupal7/sites/all/modules/ctools/plugins/content_types/node_form/node_form_language.inc b/drupal7/sites/all/modules/ctools/plugins/content_types/node_form/node_form_language.inc
index 76ab3f82..dfef295f 100644
--- a/drupal7/sites/all/modules/ctools/plugins/content_types/node_form/node_form_language.inc
+++ b/drupal7/sites/all/modules/ctools/plugins/content_types/node_form/node_form_language.inc
@@ -17,7 +17,7 @@ $plugin = array(
 
 function ctools_node_form_language_content_type_render($subtype, $conf, $panel_args, &$context) {
   $block = new stdClass();
-  $block->module = t('node_form');
+  $block->module = 'node_form';
 
   $block->delta = 'language-options';
 
diff --git a/drupal7/sites/all/modules/ctools/plugins/content_types/node_form/node_form_log.inc b/drupal7/sites/all/modules/ctools/plugins/content_types/node_form/node_form_log.inc
index 329ea899..aac42a64 100644
--- a/drupal7/sites/all/modules/ctools/plugins/content_types/node_form/node_form_log.inc
+++ b/drupal7/sites/all/modules/ctools/plugins/content_types/node_form/node_form_log.inc
@@ -17,7 +17,7 @@ $plugin = array(
 
 function ctools_node_form_log_content_type_render($subtype, $conf, $panel_args, &$context) {
   $block = new stdClass();
-  $block->module = t('node_form');
+  $block->module = 'node_form';
   $block->title = t('Revision information');
 
   if (isset($context->form)) {
diff --git a/drupal7/sites/all/modules/ctools/plugins/content_types/node_form/node_form_menu.inc b/drupal7/sites/all/modules/ctools/plugins/content_types/node_form/node_form_menu.inc
index 576dcd43..9b4df434 100644
--- a/drupal7/sites/all/modules/ctools/plugins/content_types/node_form/node_form_menu.inc
+++ b/drupal7/sites/all/modules/ctools/plugins/content_types/node_form/node_form_menu.inc
@@ -21,7 +21,7 @@ if (module_exists('menu')) {
 
 function ctools_node_form_menu_content_type_render($subtype, $conf, $panel_args, &$context) {
   $block = new stdClass();
-  $block->module = t('node_form');
+  $block->module = 'node_form';
 
   $block->title = t('Menu options');
   $block->delta = 'menu-options';
diff --git a/drupal7/sites/all/modules/ctools/plugins/content_types/node_form/node_form_path.inc b/drupal7/sites/all/modules/ctools/plugins/content_types/node_form/node_form_path.inc
index 9aeeba00..262851f0 100644
--- a/drupal7/sites/all/modules/ctools/plugins/content_types/node_form/node_form_path.inc
+++ b/drupal7/sites/all/modules/ctools/plugins/content_types/node_form/node_form_path.inc
@@ -21,7 +21,7 @@ if (module_exists('path')) {
 
 function ctools_node_form_path_content_type_render($subtype, $conf, $panel_args, &$context) {
   $block = new stdClass();
-  $block->module = t('node_form');
+  $block->module = 'node_form';
 
   $block->title = t('URL path options');
   $block->delta = 'url-path-options';
diff --git a/drupal7/sites/all/modules/ctools/plugins/content_types/node_form/node_form_publishing.inc b/drupal7/sites/all/modules/ctools/plugins/content_types/node_form/node_form_publishing.inc
index f8b92aec..53b584f9 100644
--- a/drupal7/sites/all/modules/ctools/plugins/content_types/node_form/node_form_publishing.inc
+++ b/drupal7/sites/all/modules/ctools/plugins/content_types/node_form/node_form_publishing.inc
@@ -23,7 +23,7 @@ function ctools_node_form_publishing_content_type_render($subtype, $conf, $panel
   $block = new stdClass();
 
   $block->title = t('Publishing options');
-  $block->module = t('node_form');
+  $block->module = 'node_form';
   $block->delta = 'publishing-options';
 
   if (isset($context->form)) {
diff --git a/drupal7/sites/all/modules/ctools/plugins/content_types/node_form/node_form_title.inc b/drupal7/sites/all/modules/ctools/plugins/content_types/node_form/node_form_title.inc
index 2f58ee30..14e79aba 100644
--- a/drupal7/sites/all/modules/ctools/plugins/content_types/node_form/node_form_title.inc
+++ b/drupal7/sites/all/modules/ctools/plugins/content_types/node_form/node_form_title.inc
@@ -17,7 +17,7 @@ $plugin = array(
 
 function ctools_node_form_title_content_type_render($subtype, $conf, $panel_args, &$context) {
   $block = new stdClass();
-  $block->module = t('node_form');
+  $block->module = 'node_form';
 
   $block->delta = 'title-options';
 
diff --git a/drupal7/sites/all/modules/ctools/plugins/contexts/user.inc b/drupal7/sites/all/modules/ctools/plugins/contexts/user.inc
index 362cfe3b..bbdbd1a2 100644
--- a/drupal7/sites/all/modules/ctools/plugins/contexts/user.inc
+++ b/drupal7/sites/all/modules/ctools/plugins/contexts/user.inc
@@ -152,6 +152,7 @@ function ctools_context_user_settings_form_submit($form, &$form_state) {
  */
 function ctools_context_user_convert_list() {
   $tokens = token_info();
+  $list = array();
   foreach ($tokens['tokens']['user'] as $id => $info) {
     if (!isset($list[$id])) {
       $list[$id] = $info['name'];
diff --git a/drupal7/sites/all/modules/ctools/plugins/export_ui/ctools_export_ui.class.php b/drupal7/sites/all/modules/ctools/plugins/export_ui/ctools_export_ui.class.php
index 3e1aed4b..0c8145e4 100644
--- a/drupal7/sites/all/modules/ctools/plugins/export_ui/ctools_export_ui.class.php
+++ b/drupal7/sites/all/modules/ctools/plugins/export_ui/ctools_export_ui.class.php
@@ -644,6 +644,7 @@ class ctools_export_ui {
   }
 
   public function add_page($js, $input, $step = NULL) {
+    $args = func_get_args();
     drupal_set_title($this->get_page_title('add'), PASS_THROUGH);
 
     // If a step not set, they are trying to create a new item. If a step
@@ -666,7 +667,7 @@ class ctools_export_ui {
       'no_redirect' => TRUE,
       'step' => $step,
       // Store these in case additional args are needed.
-      'function args' => func_get_args(),
+      'function args' => $args,
     );
 
     $output = $this->edit_execute_form($form_state);
@@ -681,6 +682,7 @@ class ctools_export_ui {
    * Main entry point to edit an item.
    */
   public function edit_page($js, $input, $item, $step = NULL) {
+    $args = func_get_args();
     drupal_set_title($this->get_page_title('edit', $item), PASS_THROUGH);
 
     // Check to see if there is a cached item to get if we're using the wizard.
@@ -702,7 +704,7 @@ class ctools_export_ui {
       'no_redirect' => TRUE,
       'step' => $step,
       // Store these in case additional args are needed.
-      'function args' => func_get_args(),
+      'function args' => $args,
     );
 
     $output = $this->edit_execute_form($form_state);
@@ -717,6 +719,7 @@ class ctools_export_ui {
    * Main entry point to clone an item.
    */
   public function clone_page($js, $input, $original, $step = NULL) {
+    $args = func_get_args();
     drupal_set_title($this->get_page_title('clone', $original), PASS_THROUGH);
 
     // If a step not set, they are trying to create a new clone. If a step
@@ -756,7 +759,7 @@ class ctools_export_ui {
       'no_redirect' => TRUE,
       'step' => $step,
       // Store these in case additional args are needed.
-      'function args' => func_get_args(),
+      'function args' => $args,
     );
 
     $output = $this->edit_execute_form($form_state);
@@ -1237,6 +1240,7 @@ class ctools_export_ui {
    * Page callback to import information for an exportable item.
    */
   public function import_page($js, $input, $step = NULL) {
+    $args = func_get_args();
     drupal_set_title($this->get_page_title('import'), PASS_THROUGH);
     // Import is basically a multi step wizard form, so let's go ahead and
     // use CTools' wizard.inc for it.
@@ -1261,7 +1265,7 @@ class ctools_export_ui {
       'no_redirect' => TRUE,
       'step' => $step,
       // Store these in case additional args are needed.
-      'function args' => func_get_args(),
+      'function args' => $args,
     );
 
     // import always uses the wizard.
diff --git a/drupal7/sites/all/modules/ctools/stylizer/stylizer.info b/drupal7/sites/all/modules/ctools/stylizer/stylizer.info
index f8b442e4..90204a3d 100644
--- a/drupal7/sites/all/modules/ctools/stylizer/stylizer.info
+++ b/drupal7/sites/all/modules/ctools/stylizer/stylizer.info
@@ -2,13 +2,11 @@ name = Stylizer
 description = Create custom styles for applications such as Panels.
 core = 7.x
 package = Chaos tool suite
-version = CTOOLS_MODULE_VERSION
 dependencies[] = ctools
 dependencies[] = color
 
-; Information added by Drupal.org packaging script on 2018-02-24
-version = "7.x-1.14"
+; Information added by Drupal.org packaging script on 2019-02-08
+version = "7.x-1.15"
 core = "7.x"
 project = "ctools"
-datestamp = "1519455788"
-
+datestamp = "1549603691"
diff --git a/drupal7/sites/all/modules/ctools/term_depth/term_depth.info b/drupal7/sites/all/modules/ctools/term_depth/term_depth.info
index 326b2aef..4baee51d 100644
--- a/drupal7/sites/all/modules/ctools/term_depth/term_depth.info
+++ b/drupal7/sites/all/modules/ctools/term_depth/term_depth.info
@@ -3,11 +3,9 @@ description = Controls access to context based upon term depth
 core = 7.x
 dependencies[] = ctools
 package = Chaos tool suite
-version = CTOOLS_MODULE_VERSION
 
-; Information added by Drupal.org packaging script on 2018-02-24
-version = "7.x-1.14"
+; Information added by Drupal.org packaging script on 2019-02-08
+version = "7.x-1.15"
 core = "7.x"
 project = "ctools"
-datestamp = "1519455788"
-
+datestamp = "1549603691"
diff --git a/drupal7/sites/all/modules/ctools/tests/ctools_export_test/ctools_export_test.info b/drupal7/sites/all/modules/ctools/tests/ctools_export_test/ctools_export_test.info
index 7abac58c..a2d0efbd 100644
--- a/drupal7/sites/all/modules/ctools/tests/ctools_export_test/ctools_export_test.info
+++ b/drupal7/sites/all/modules/ctools/tests/ctools_export_test/ctools_export_test.info
@@ -2,15 +2,13 @@ name = CTools export test
 description = CTools export test module
 core = 7.x
 package = Chaos tool suite
-version = CTOOLS_MODULE_VERSION
 dependencies[] = ctools
 hidden = TRUE
 
 files[] = ctools_export.test
 
-; Information added by Drupal.org packaging script on 2018-02-24
-version = "7.x-1.14"
+; Information added by Drupal.org packaging script on 2019-02-08
+version = "7.x-1.15"
 core = "7.x"
 project = "ctools"
-datestamp = "1519455788"
-
+datestamp = "1549603691"
diff --git a/drupal7/sites/all/modules/ctools/tests/ctools_plugin_test.info b/drupal7/sites/all/modules/ctools/tests/ctools_plugin_test.info
index 649e8912..d51a5261 100644
--- a/drupal7/sites/all/modules/ctools/tests/ctools_plugin_test.info
+++ b/drupal7/sites/all/modules/ctools/tests/ctools_plugin_test.info
@@ -1,14 +1,12 @@
 name = Chaos tools plugins test
 description = Provides hooks for testing ctools plugins.
 package = Chaos tool suite
-version = CTOOLS_MODULE_VERSION
 core = 7.x
 dependencies[] = ctools
 hidden = TRUE
 
-; Information added by Drupal.org packaging script on 2018-02-24
-version = "7.x-1.14"
+; Information added by Drupal.org packaging script on 2019-02-08
+version = "7.x-1.15"
 core = "7.x"
 project = "ctools"
-datestamp = "1519455788"
-
+datestamp = "1549603691"
diff --git a/drupal7/sites/all/modules/ctools/views_content/plugins/relationships/node_from_view.inc b/drupal7/sites/all/modules/ctools/views_content/plugins/relationships/node_from_view.inc
index 0f3fa8e0..f54cbc6b 100644
--- a/drupal7/sites/all/modules/ctools/views_content/plugins/relationships/node_from_view.inc
+++ b/drupal7/sites/all/modules/ctools/views_content/plugins/relationships/node_from_view.inc
@@ -33,7 +33,7 @@ function views_content_node_from_view_context($context, $conf, $placeholder = FA
   views_content_context_get_output($context);
 
   $row = intval($conf['row']) - 1;
-  if (isset($view->result[$row])) {
+  if (isset($view->result[$row]) && isset($view->base_field) && isset($view->result[$row]->{$view->base_field})) {
     $nid = $view->result[$row]->{$view->base_field};
     if ($nid) {
       $node = node_load($nid);
diff --git a/drupal7/sites/all/modules/ctools/views_content/views_content.info b/drupal7/sites/all/modules/ctools/views_content/views_content.info
index 254d474b..68f4d875 100644
--- a/drupal7/sites/all/modules/ctools/views_content/views_content.info
+++ b/drupal7/sites/all/modules/ctools/views_content/views_content.info
@@ -5,14 +5,12 @@ dependencies[] = ctools
 dependencies[] = views
 core = 7.x
 package = Chaos tool suite
-version = CTOOLS_MODULE_VERSION
 files[] = plugins/views/views_content_plugin_display_ctools_context.inc
 files[] = plugins/views/views_content_plugin_display_panel_pane.inc
 files[] = plugins/views/views_content_plugin_style_ctools_context.inc
 
-; Information added by Drupal.org packaging script on 2018-02-24
-version = "7.x-1.14"
+; Information added by Drupal.org packaging script on 2019-02-08
+version = "7.x-1.15"
 core = "7.x"
 project = "ctools"
-datestamp = "1519455788"
-
+datestamp = "1549603691"
diff --git a/drupal7/sites/all/modules/nodeaccess/CHANGELOG.txt b/drupal7/sites/all/modules/nodeaccess/CHANGELOG.txt
index c7673d80..5cd30ff3 100644
--- a/drupal7/sites/all/modules/nodeaccess/CHANGELOG.txt
+++ b/drupal7/sites/all/modules/nodeaccess/CHANGELOG.txt
@@ -1,5 +1,22 @@
+Nodeaccess 7.x-1.6
+==================
+Corrected issues surrounding security advisory SA-CONTRIB-2019-009
+Addressed compatibility issues with SQL queries regarding PostgreSQL
+Rebuilding of node permissions required after update.
+
 Nodeaccess 7.x-1.x
 ===================
+Issue #2427345 by trigdog: fix Database Error - nodeaccess_author duplicate entry
+
+Nodeaccess 7.x-1.5
+===================
+Add SimpleTest automated testing.
+Issue #2037899 by vlad.pavlovic: Expose operations as API.
+Issue #2334555 by joelpittet: Casting consistency (boolean) to (bool).
+Issue #2342327 by willzyx: node_access_write_grants should not be called directly.
+Issue #2228713 by willzyx: Unpublished nodes are visible.
+Issue #2140819 by justdave: "Preserve hidden grants" setting ignored.
+Issue #2364463 by nathanweeks, vlad.pavlovic: Incorrect SQL list quoting causes PostgreSQL error.
 
 Nodeaccess 7.x-1.4
 ===================
diff --git a/drupal7/sites/all/modules/nodeaccess/nodeaccess.admin.inc b/drupal7/sites/all/modules/nodeaccess/nodeaccess.admin.inc
new file mode 100644
index 00000000..d4fb284f
--- /dev/null
+++ b/drupal7/sites/all/modules/nodeaccess/nodeaccess.admin.inc
@@ -0,0 +1,442 @@
+<?php
+
+/**
+ * @file Nodeaccess admin forms.
+ */
+
+/**
+ * Menu callback. Draws the admin page.
+ */
+function nodeaccess_admin() {
+  return drupal_get_form('nodeaccess_admin_form');
+}
+
+/**
+ * Menu callback. Draws the admin page.
+ *
+ * @param $form
+ * @param $form_state
+ *
+ * @return array $form
+ */
+function nodeaccess_admin_form($form, $form_state) {
+  // Set defaults from variable_get.
+  $show = variable_get('nodeaccess-types', array());
+  $roles = nodeaccess_get_role_aliases();
+  $allowed_roles = variable_get('nodeaccess-roles', array());
+  $allowed_grants = variable_get('nodeaccess-grants', array());
+
+  $form['priority'] = array(
+    '#type' => 'checkbox',
+    '#title' => t('Give node grants priority'),
+    '#default_value' => variable_get('nodeaccess-priority', 0),
+    '#description' => t('If you are only using this access control module,
+      you can safely ignore this. If you are using multiple access control
+      modules, and you want the grants given on individual nodes to override
+      any grants given by other modules, you should check this box.'),
+  );
+
+  // Select whether to preserve hidden grants.
+  $form['preserve'] = array(
+    '#type' => 'checkbox',
+    '#title' => t('Preserve hidden grants'),
+    '#default_value' => variable_get('nodeaccess-preserve', 1),
+    '#description' => '<small>' . t('If you check this box, any hidden grants
+      are preserved when you save grants. Otherwise all grants users are not
+      allowed to view or edit are revoked on save.') . '</small>',
+  );
+
+  // Select permissions you want to allow users to view and edit.
+  $form['grant'] = array(
+    '#type' => 'fieldset',
+    '#collapsible' => TRUE,
+    '#collapsed' => TRUE,
+    '#title' => t('Allowed Grants'),
+    '#tree' => TRUE,
+    '#description' => '<small>' . t('The selected grants will be listed on
+      individual node grants. If you wish for certain grants to be hidden from
+      users on the node grants tab, make sure they are not selected here.') .
+      '</small>',
+  );
+  $form['grant']['view'] = array(
+    '#type' => 'checkbox',
+    '#title' => t('View'),
+    '#default_value' => $allowed_grants['view'],
+  );
+  $form['grant']['edit'] = array(
+    '#type' => 'checkbox',
+    '#title' => t('Edit'),
+    '#default_value' => $allowed_grants['edit'],
+  );
+  $form['grant']['delete'] = array(
+    '#type' => 'checkbox',
+    '#title' => t('Delete'),
+    '#default_value' => $allowed_grants['delete'],
+  );
+
+  // Select roles the permissions of which you want to allow users to
+  // view and edit, and the aliases and weights of those roles.
+  $form['role'] = array(
+    '#type' => 'fieldset',
+    '#collapsible' => TRUE,
+    '#collapsed' => TRUE,
+    '#title' => t('Allowed Roles'),
+    '#tree' => TRUE,
+    '#theme' => 'nodeaccess_admin_form_roles',
+    '#description' => t('The selected roles will be listed on individual node
+      grants. If you wish for certain roles to be hidden from users on the node
+      grants tab, make sure they are not selected here. You may also provide an
+      alias for each role to be displayed to the user and a weight to order them
+      by. This is useful if your roles have machine-readable names not intended
+      for human users.'),
+  );
+
+  foreach ($roles as $id => $role) {
+    // Catch NULL values.
+    if (!$role['alias']) {
+      $role['alias'] = '';
+    }
+    if (!$role['weight']) {
+      $role['weight'] = 0;
+    }
+    $form['role'][$id]['name'] = array(
+      '#type' => 'hidden',
+      '#value' => $role['name'],
+    );
+    $form['role'][$id]['allow'] = array(
+      '#type' => 'checkbox',
+      '#title' => check_plain($role['name']),
+      '#default_value' => isset($allowed_roles[$id]) ? $allowed_roles[$id] : 0,
+    );
+    $form['role'][$id]['alias'] = array(
+      '#type' => 'textfield',
+      '#default_value' => $role['alias'],
+      '#size' => 50,
+      '#maxlength' => 50,
+    );
+    $form['role'][$id]['weight'] = array(
+      '#type' => 'weight',
+      '#default_value' => $role['weight'],
+      '#delta' => 10,
+    );
+  }
+
+  // Grant tab to node types.
+  $form['nodeaccess']['tabs'] = array(
+    '#type' => 'fieldset',
+    '#collapsible' => TRUE,
+    '#collapsed' => TRUE,
+    '#title' => t('Content Type Grant Tab Access'),
+    '#tree' => TRUE,
+    '#description' => t('Show grant tab for the following node types.'),
+  );
+
+  $options = array();
+  foreach (node_type_get_types() as $type => $bundle) {
+    $options[$type] = array(
+      'content_type' => check_plain($bundle->name),
+    );
+  }
+  $form['nodeaccess']['tabs']['show'] = array(
+    '#type' => 'tableselect',
+    '#header' => array(
+      'content_type' => t('Content type'),
+    ),
+    '#options' => $options,
+    '#default_value' => $show,
+    '#empty' => t('No content types to add a grant tab.'),
+  );
+
+  // Generate fieldsets for each node type.
+  foreach (node_type_get_types() as $type => $bundle) {
+    $form['nodeaccess'][$type] = array(
+      '#type' => 'fieldset',
+      '#collapsible' => TRUE,
+      '#collapsed' => TRUE,
+      '#title' => check_plain($bundle->name),
+      '#tree' => TRUE,
+      '#theme' => 'nodeaccess_admin_form_types',
+    );
+
+    // Set default author permissions for node type.
+    $author_prefs = variable_get('nodeaccess_authors', array());
+    $form['nodeaccess'][$type]['author']['grant_view'] = array(
+      '#type' => 'checkbox',
+      '#default_value' => $author_prefs[$type]['grant_view'],
+    );
+    $form['nodeaccess'][$type]['author']['grant_update'] = array(
+      '#type' => 'checkbox',
+      '#default_value' => $author_prefs[$type]['grant_update'],
+    );
+    $form['nodeaccess'][$type]['author']['grant_delete'] = array(
+      '#type' => 'checkbox',
+      '#default_value' => $author_prefs[$type]['grant_delete'],
+    );
+
+    $perms = variable_get('nodeaccess_' . $type, array());
+    foreach ($perms as $perm) {
+      $opts[$perm['gid']] = $perm;
+    }
+
+    // Set default role permissions for node type.
+    foreach (user_roles() as $id => $role) {
+      $form['nodeaccess'][$type]['roles'][$id]['name'] = array('#markup' => $role);
+      $form['nodeaccess'][$type]['roles'][$id]['grant_view'] = array(
+        '#type' => 'checkbox',
+        '#default_value' => isset($opts[$id]['grant_view']) ? $opts[$id]['grant_view'] : 0,
+      );
+      $form['nodeaccess'][$type]['roles'][$id]['grant_update'] = array(
+        '#type' => 'checkbox',
+        '#default_value' => isset($opts[$id]['grant_update']) ? $opts[$id]['grant_update'] : 0,
+      );
+      $form['nodeaccess'][$type]['roles'][$id]['grant_delete'] = array(
+        '#type' => 'checkbox',
+        '#default_value' => isset($opts[$id]['grant_delete']) ? $opts[$id]['grant_delete'] : 0,
+      );
+    }
+
+    // Set the default permissions if userreference exists and is enabled on
+    // the content type.
+    if (module_exists('user_reference')) {
+      $bundle = field_extract_bundle('node', $bundle);
+      $fields = field_info_instances('node', $bundle);
+      $user_reference_perms = variable_get('nodeaccess_' . $type . '_user_reference', array());
+
+      $field_types = field_info_field_types();
+
+      foreach ($fields as $field) {
+        $field = field_info_field($field['field_name']);
+        if ($field['type'] == 'user_reference') {
+          $enabled = isset($user_reference_perms[$field['field_name']]['enabled']) ?
+            $user_reference_perms[$field['field_name']]['enabled'] : 0;
+          $view = isset($user_reference_perms[$field['field_name']]['grant_view']) ?
+            $user_reference_perms[$field['field_name']]['grant_view'] : 0;
+          $update = isset($user_reference_perms[$field['field_name']]['grant_update']) ?
+            $user_reference_perms[$field['field_name']]['grant_update'] : 0;
+          $delete = isset($user_reference_perms[$field['field_name']]['grant_delete']) ?
+            $user_reference_perms[$field['field_name']]['grant_delete'] : 0;
+
+          $form['nodeaccess'][$type]['user_reference'][$field['field_name']]['name'] = array(
+            '#value' => t($field_types[$field['type']]['label']),
+          );
+          $form['nodeaccess'][$type]['user_reference'][$field['field_name']]['enabled'] = array(
+            '#type' => 'checkbox',
+            '#default_value' => $enabled,
+          );
+          $form['nodeaccess'][$type]['user_reference'][$field['field_name']]['grant_view'] = array(
+            '#type' => 'checkbox',
+            '#default_value' => $view,
+          );
+          $form['nodeaccess'][$type]['user_reference'][$field['field_name']]['grant_update'] = array(
+            '#type' => 'checkbox',
+            '#default_value' => $update,
+          );
+          $form['nodeaccess'][$type]['user_reference'][$field['field_name']]['grant_delete'] = array(
+            '#type' => 'checkbox',
+            '#default_value' => $delete,
+          );
+        }
+      }
+    }
+  }
+  $form['submit'] = array(
+    '#type' => 'submit',
+    '#value' => t('Save Grants'),
+  );
+  return $form;
+}
+
+/**
+ * Submit function for nodeaccess_admin_form.
+ *
+ * @param $form
+ * @param $form_state
+ */
+function nodeaccess_admin_form_submit($form, $form_state) {
+  $form_values = $form_state['values'];
+  $allowed_grants = array();
+  // Save priority.
+  variable_set('nodeaccess-priority', $form_values['priority']);
+  // Save preserve.
+  variable_set('nodeaccess-preserve', $form_values['preserve']);
+  // Save allowed grants.
+  foreach ($form_values['grant'] as $id => $val) {
+    $allowed_grants[$id] = $val;
+  }
+  variable_set('nodeaccess-grants', $allowed_grants);
+  // Save allowed roles, role aliases and weights.
+  $alias_prefs = array();
+  $allowed_roles = array();
+  foreach ($form_values['role'] as $id => $val) {
+    $allowed_roles[$id] = $val['allow'];
+    // Save alias and weight only for allowed roles.
+    if ($val['allow']) {
+      // If alias is empty, default to role name.
+      if ($val['alias']) {
+        $alias_prefs[$id]['name'] = $val['alias'];
+      }
+      else {
+        $alias_prefs[$id]['name'] = $val['name'];
+      }
+      $alias_prefs[$id]['weight'] = $val['weight'];
+    }
+    else {
+      // Otherwise, we only save alias if one was specified.
+      if ($val['alias']) {
+        $alias_prefs[$id]['name'] = $val['alias'];
+        $alias_prefs[$id]['weight'] = $val['weight'];
+      }
+    }
+  }
+  variable_set('nodeaccess-roles', $allowed_roles);
+  nodeaccess_save_role_aliases($alias_prefs);
+  // Save author and role permissions for each node type.
+  $author_prefs = array();
+  foreach (node_type_get_types() as $type => $name) {
+    $grants = array();
+    foreach ($form_values[$type]['roles'] as $role => $val) {
+      $grants[] = array(
+        'gid' => $role,
+        'realm' => 'nodeaccess_rid',
+        'grant_view' => $val['grant_view'],
+        'grant_update' => $val['grant_update'],
+        'grant_delete' => $val['grant_delete'],
+      );
+    }
+    variable_set('nodeaccess_' . $type, $grants);
+    $author_prefs[$type] = $form_values[$type]['author'];
+    // Also save userreference default permissions if enabled.
+    if (module_exists('user_reference') && isset($form_values[$type]['user_reference'])) {
+      $user_reference_grants = array();
+      foreach ($form_values[$type]['user_reference'] as $user_reference_field => $val) {
+        $user_reference_grants[$user_reference_field] = array(
+          'gid' => 'nodeaccess_uid',
+          'enabled' => $val['enabled'],
+          'grant_view' => $val['grant_view'],
+          'grant_update' => $val['grant_update'],
+          'grant_delete' => $val['grant_delete'],
+        );
+      }
+      variable_set('nodeaccess_' . $type . '_user_reference', $user_reference_grants);
+    }
+  }
+  variable_set('nodeaccess_authors', $author_prefs);
+
+  // Save allowed node type grant tab.
+  $allowed_types = array();
+  foreach ($form_values['tabs']['show'] as $type => $value) {
+    $allowed_types[$type] = (bool) $value;
+  }
+
+  nodeaccess_set_type_grants($allowed_types);
+
+  drupal_set_message(t('Grants saved.'));
+}
+
+/**
+ * Theme function for nodeaccess_admin_form_roles.
+ *
+ * @param $variables
+ * @throws Exception
+ * @return string
+ */
+function theme_nodeaccess_admin_form_roles($variables) {
+  $output = '';
+  $form = $variables['form'];
+  $rows = array();
+  foreach (element_children($form) as $rid) {
+    // Classify the weight element for TableDrag.
+    $form[$rid]['weight']['#attributes']['class'] = array('roles-order-weight');
+
+    // Mark the table row as draggable for TableDrag.
+    $row = array(
+      'data' => array(),
+      'class' => array('draggable'),
+    );
+    // Render the table columns.
+    $row['data'][] = drupal_render($form[$rid]['allow']);
+    $row['data'][] = drupal_render($form[$rid]['alias']);
+    $row['data'][] = drupal_render($form[$rid]['weight']);
+    $rows[] = $row;
+  }
+
+  $header = array(t('Allow Role'), t('Alias'), t('Weight'));
+  $output .= theme('table', array(
+    'header' => $header,
+    'rows' => $rows,
+    'attributes' => array('id' => 'roles-order'),
+  ));
+  $output .= drupal_render_children($form);
+
+  // Attach TableDrag to the table ID and contained weight elements.
+  drupal_add_tabledrag('roles-order', 'order', 'sibling', 'roles-order-weight');
+
+  return $output;
+}
+
+
+/**
+ * Theme function to render admin form.
+ *
+ * @param $variables
+ *
+ * @return string
+ */
+function theme_nodeaccess_admin_form_types($variables) {
+  $form = $variables['form'];
+  $output = drupal_render($form['show']);
+  $roles = element_children($form['roles']);
+  $header = array(t('Role'), t('View'), t('Edit'), t('Delete'));
+  $rows = array();
+  foreach ($roles as $role) {
+    $row = array();
+    $row[] = drupal_render($form['roles'][$role]['name']);
+    $row[] = drupal_render($form['roles'][$role]['grant_view']);
+    $row[] = drupal_render($form['roles'][$role]['grant_update']);
+    $row[] = drupal_render($form['roles'][$role]['grant_delete']);
+    $rows[] = $row;
+  }
+  $output .= theme('table', array('header' => $header, 'rows' => $rows));
+  $header = array(t('Author Settings'), t('View'), t('Edit'), t('Delete'));
+  $row = array();
+  $row[] = t('Node author');
+  $row[] = drupal_render($form['author']['grant_view']);
+  $row[] = drupal_render($form['author']['grant_update']);
+  $row[] = drupal_render($form['author']['grant_delete']);
+
+  $output .= theme('table', array('header' => $header, 'rows' => array($row)));
+
+  $output .= '<small>' . t('The settings selected for the node author will
+    define what permissions the node author has. This cannot be changed on
+    individual node grants.') . '</small>';
+
+  if (module_exists('user_reference') && isset($form['user_reference'])) {
+    $user_reference_fields = element_children($form['user_reference']);
+    $header = array(t('User Reference Field'),
+      t('Enable this field'),
+      t('View'),
+      t('Edit'),
+      t('Delete'),
+    );
+    $rows = array();
+    foreach ($user_reference_fields as $user_reference_field) {
+      $row = array();
+
+      $row[] = $form['user_reference'][$user_reference_field]['name']['#value'];
+      $row[] = drupal_render($form['user_reference'][$user_reference_field]['enabled']);
+      $row[] = drupal_render($form['user_reference'][$user_reference_field]['grant_view']);
+      $row[] = drupal_render($form['user_reference'][$user_reference_field]['grant_update']);
+      $row[] = drupal_render_children($form['user_reference'][$user_reference_field]['grant_delete']);
+      $rows[] = $row;
+    }
+    $output .= theme('table', array(
+      'header' => $header,
+      'rows' => $rows,
+    ));
+    $output .= '<small>' . t('If enabled, the value of the user reference field
+      will be granted the associated permissions. If a user changes the value of
+      the user reference field on a node, the associated user will be modified
+      in the node-specific access table.') . '</small>';
+  }
+  return $output;
+}
diff --git a/drupal7/sites/all/modules/nodeaccess/nodeaccess.api.php b/drupal7/sites/all/modules/nodeaccess/nodeaccess.api.php
new file mode 100644
index 00000000..5c1e10d1
--- /dev/null
+++ b/drupal7/sites/all/modules/nodeaccess/nodeaccess.api.php
@@ -0,0 +1,50 @@
+<?php
+
+/**
+ * @file
+ * API documentation for Nodeaccess module.
+ */
+
+
+/**
+ * Allow for changes to be made on the entire grants array for a node before
+ * saving.
+ *
+ * @param array &$grants
+ *   array(
+ *    array(
+ *      'gid' => (int) gid for realm,
+ *      'realm' => (string) what realm the access grant belongs to (ex: nodeaccess_rid).
+ *      'grant_view' => (int) view access being granted,
+ *      'grant_update' => (int) update access being granted,
+ *      'grant_delete' => (int) delete access being granted,
+ *    ),
+ *    ...
+ *  )
+ * @param $node
+ *   Node object the grant is being applied to.
+ */
+function hook_nodeaccess_grants_alter(&$grants, $node) {
+  foreach ($grants as $gid => $grant) {
+    // Make some changes to the grants.
+  }
+}
+
+
+/**
+ * Allow for changes to be made on the single grant item prior to saving.
+ *
+ * @param array &$grant
+ *  array(
+ *    'gid' => (int) gid for realm,
+ *    'realm' => (string) what realm the access grant belongs to (ex: nodeaccess_rid).
+ *    'grant_view' => (int) view access being granted,
+ *    'grant_update' => (int) update access being granted,
+ *    'grant_delete' => (int) delete access being granted,
+ *  )
+ * @param $node
+ *   Node object the grant is being applied to.
+ */
+function hook_nodeaccess_grant_alter(&$grant, $node) {
+  // Make some change to the individual grant.
+}
diff --git a/drupal7/sites/all/modules/nodeaccess/nodeaccess.info b/drupal7/sites/all/modules/nodeaccess/nodeaccess.info
index f361bb35..223850be 100644
--- a/drupal7/sites/all/modules/nodeaccess/nodeaccess.info
+++ b/drupal7/sites/all/modules/nodeaccess/nodeaccess.info
@@ -4,9 +4,12 @@ core = 7.x
 package = Access control
 configure = admin/config/people/nodeaccess
 
-; Information added by Drupal.org packaging script on 2014-10-06
-version = "7.x-1.4"
+files[] = tests/nodeaccess_grant.test
+files[] = tests/nodeaccess_role.test
+files[] = tests/nodeaccess_publish.test
+
+; Information added by Drupal.org packaging script on 2019-02-09
+version = "7.x-1.6"
 core = "7.x"
 project = "nodeaccess"
-datestamp = "1412639629"
-
+datestamp = "1549753983"
diff --git a/drupal7/sites/all/modules/nodeaccess/nodeaccess.install b/drupal7/sites/all/modules/nodeaccess/nodeaccess.install
index d0f987c7..edd207a0 100644
--- a/drupal7/sites/all/modules/nodeaccess/nodeaccess.install
+++ b/drupal7/sites/all/modules/nodeaccess/nodeaccess.install
@@ -164,3 +164,10 @@ function nodeaccess_uninstall() {
   // Remove tables.
   drupal_uninstall_schema('nodeaccess');
 }
+
+/**
+ * Flag nodeaccess permissions as needing a rebuild. Rebuild permissions from the status page.
+ */
+function nodeaccess_update_7105() {
+  node_access_needs_rebuild();
+}
diff --git a/drupal7/sites/all/modules/nodeaccess/nodeaccess.module b/drupal7/sites/all/modules/nodeaccess/nodeaccess.module
index 781bac80..19335d20 100644
--- a/drupal7/sites/all/modules/nodeaccess/nodeaccess.module
+++ b/drupal7/sites/all/modules/nodeaccess/nodeaccess.module
@@ -5,51 +5,74 @@
  * Provide per node access control
  */
 
+
 /**
  * Implements hook_help().
+ *
+ * @param string $path
+ * @param $arg
+ *
+ * @return string|NULL
  */
 function nodeaccess_help($path, $arg) {
   switch ($path) {
     case 'node/%/grant':
-      return t("You can set grants for individual users. Enter a name or a partial name in the box and click Search or press return. You must check the 'Keep?' checkbox if you want to keep the user for granting. Note that user grants are in addition to those coming from roles.");
+      return t("You can set grants for individual users. Enter a name or a
+        partial name in the box and click Search or press return. You must check
+        the 'Keep?' checkbox if you want to keep the user for granting. Note
+        that user grants are in addition to those coming from roles.");
   }
 }
 
+
 /**
  * Implements hook_menu().
+ *
+ * @return array
  */
 function nodeaccess_menu() {
+  $items = array();
+
   $items['admin/config/people/nodeaccess'] = array(
     'title' => 'Nodeaccess',
     'description' => 'Change default settings for the Nodeaccess module.',
     'page callback' => 'nodeaccess_admin',
+    'file' => 'nodeaccess.admin.inc',
     'access arguments' => array('administer nodeaccess'),
   );
+
   $items['node/%node/grant'] = array(
     'title' => 'Grant',
-    'page callback' => 'nodeaccess_grants',
+    'page callback' => 'nodeaccess_grant_tab',
     'page arguments' => array(1),
     'access callback' => 'nodeaccess_access',
     'access arguments' => array('grant', 1),
     'weight' => 5,
     'type' => MENU_LOCAL_TASK,
   );
+
   return $items;
 }
 
+
 /**
  * Implements hook_admin_paths().
+ *
+ * @return array
  */
 function nodeaccess_admin_paths() {
   $paths = array(
     'node/*/grant' => TRUE,
     'admin/config/people/nodeaccess' => TRUE,
   );
+
   return $paths;
 }
 
 /**
  * Implements hook_permission().
+ *
+ * @return array
  */
 function nodeaccess_permission() {
 
@@ -72,10 +95,18 @@ function nodeaccess_permission() {
   );
 }
 
+
 /**
  * Implements hook_node_access().
+ *
+ * @param $node
+ * @param $op
+ * @param $account
+ *
+ * @return string
  */
 function nodeaccess_node_access($node, $op, $account) {
+
   switch ($op) {
     case 'update':
     case 'delete':
@@ -85,52 +116,39 @@ function nodeaccess_node_access($node, $op, $account) {
       }
       // If the node belongs to a deleted user.
       if ($account->uid == 0 && $node->uid == 0) {
+        $grants = nodeaccess_get_grants($node);
         // We check if the role has particular access to this node.
-        $grants = _nodeaccess_get_grants($node);
         // If anonymous has rights to this node, we allow them.
-        if (($grants['rid'][1]['grant_update'] && $op == 'update') ||
-            ($grants['rid'][1]['grant_delete'] && $op == 'delete')) {
+        if ((!empty($grants['rid'][DRUPAL_ANONYMOUS_RID]['grant_update']) && $op == 'update') ||
+            (!empty($grants['rid'][DRUPAL_ANONYMOUS_RID]['grant_delete']) && $op == 'delete')) {
           return NODE_ACCESS_ALLOW;
         }
         return NODE_ACCESS_DENY;
       }
       break;
-
-    case 'view':
-      if (!isset($account->uid)) {
-        global $user;
-        $account = $user;
-      }
-      // If the node is not published. We check other permissions.
-      if (!$node->status) {
-        // If the current user is the author and not anonymous.
-        if ($node->uid == $account->uid && $account->uid > 0) {
-          // We check to see if they have access to view own unpublished.
-          if (user_access('view own unpublished content') || user_access('bypass node access')) {
-            return NODE_ACCESS_ALLOW;
-          }
-        }
-        elseif (user_access('bypass node access')) {
-          return NODE_ACCESS_ALLOW;
-        }
-        return NODE_ACCESS_IGNORE;
-      }
-      break;
-
   }
 }
 
 /**
  * Determine access to Grant tab.
+ *
+ * @param $op
+ * @param $node
+ * @param $account
+ *
+ * @return bool
+ *   Whether the user has access to the grant tab.
  */
 function nodeaccess_access($op, $node, $account = NULL) {
   global $user;
   if (!$node) {
+
     return FALSE;
   }
 
   // Apparently D7 no longer defaults to admin getting anything?
   if (user_access('administer nodeaccess')) {
+
     return TRUE;
   }
 
@@ -141,20 +159,31 @@ function nodeaccess_access($op, $node, $account = NULL) {
   $allowed_types = variable_get('nodeaccess-types', array());
   if ($op == 'grant') {
     if ($node->nid && isset($allowed_types[$node->type]) && !empty($allowed_types[$node->type]) &&
-        (user_access('grant node permissions', $account) ||
-         (user_access('grant editable node permissions', $account) && node_access('update', $node, $account)) ||
-         (user_access('grant deletable node permissions', $account) && node_access('delete', $node, $account)) ||
-         (user_access('grant own node permissions', $account) && ($account->uid == $node->uid)))) {
+      (user_access('grant node permissions', $account) ||
+      (user_access('grant editable node permissions', $account) && node_access('update', $node, $account)) ||
+      (user_access('grant deletable node permissions', $account) && node_access('delete', $node, $account)) ||
+      (user_access('grant own node permissions', $account) && ($account->uid == $node->uid)))) {
+
       return TRUE;
     }
   }
+
   return FALSE;
 }
 
+
 /**
  * Implements hook_theme().
+ *
+ * @param $existing
+ * @param $type
+ * @param $theme
+ * @param $path
+ *
+ * @return array
  */
 function nodeaccess_theme($existing, $type, $theme, $path) {
+
   return array(
     'nodeaccess_admin_form_roles' => array(
       'render element'  => 'form',
@@ -168,482 +197,64 @@ function nodeaccess_theme($existing, $type, $theme, $path) {
   );
 }
 
-/**
- * Menu callback. Draws the admin page.
- */
-function nodeaccess_admin() {
-  return drupal_get_form('nodeaccess_admin_form');
-}
-
-
-/**
- * Menu callback. Draws the admin page.
- */
-function nodeaccess_admin_form($form, $form_state) {
-  // Set defaults from variable_get.
-  $show = variable_get('nodeaccess-types', array());
-  $roles = nodeaccess_get_role_aliases();
-  $allowed_roles = variable_get('nodeaccess-roles', array());
-  $allowed_grants = variable_get('nodeaccess-grants', array());
-
-  $form['priority'] = array(
-    '#type' => 'checkbox',
-    '#title' => t('Give node grants priority'),
-    '#default_value' => variable_get('nodeaccess-priority', 0),
-    '#description' => t('If you are only using this access control module, you can safely ignore this. If you are using multiple access control modules, and you want the grants given on individual nodes to override any grants given by other modules, you should check this box.'),
-  );
-
-  // Select whether to preserve hidden grants.
-  $form['preserve'] = array(
-    '#type' => 'checkbox',
-    '#title' => t('Preserve hidden grants'),
-    '#default_value' => variable_get('nodeaccess-preserve', 1),
-    '#description' => '<small>' . t('If you check this box, any hidden grants are preserved when you save grants. Otherwise all grants users are not allowed to view or edit are revoked on save.') . '</small>',
-  );
-
-  // Select permissions you want to allow users to view and edit.
-  $form['grant'] = array(
-    '#type' => 'fieldset',
-    '#collapsible' => TRUE,
-    '#collapsed' => TRUE,
-    '#title' => t('Allowed Grants'),
-    '#tree' => TRUE,
-    '#description' => '<small>' . t('The selected grants will be listed on individual node grants. If you wish for certain grants to be hidden from users on the node grants tab, make sure they are not selected here.') . '</small>',
-  );
-  $form['grant']['view'] = array(
-    '#type' => 'checkbox',
-    '#title' => t('View'),
-    '#default_value' => $allowed_grants['view'],
-  );
-  $form['grant']['edit'] = array(
-    '#type' => 'checkbox',
-    '#title' => t('Edit'),
-    '#default_value' => $allowed_grants['edit'],
-  );
-  $form['grant']['delete'] = array(
-    '#type' => 'checkbox',
-    '#title' => t('Delete'),
-    '#default_value' => $allowed_grants['delete'],
-  );
-
-  // Select roles the permissions of which you want to allow users to
-  // view and edit, and the aliases and weights of those roles.
-  $form['role'] = array(
-    '#type' => 'fieldset',
-    '#collapsible' => TRUE,
-    '#collapsed' => TRUE,
-    '#title' => t('Allowed Roles'),
-    '#tree' => TRUE,
-    '#theme' => 'nodeaccess_admin_form_roles',
-    '#description' => t('The selected roles will be listed on individual node grants. If you wish for certain roles to be hidden from users on the node grants tab, make sure they are not selected here. You may also provide an alias for each role to be displayed to the user and a weight to order them by. This is useful if your roles have machine-readable names not intended for human users.'),
-  );
-
-  foreach ($roles as $id => $role) {
-    // Catch NULL values.
-    if (!$role['alias']) {
-      $role['alias'] = '';
-    }
-    if (!$role['weight']) {
-      $role['weight'] = 0;
-    }
-    $form['role'][$id]['name'] = array(
-      '#type' => 'hidden',
-      '#value' => $role['name'],
-    );
-    $form['role'][$id]['allow'] = array(
-      '#type' => 'checkbox',
-      '#title' => check_plain($role['name']),
-      '#default_value' => isset($allowed_roles[$id]) ? $allowed_roles[$id] : 0,
-    );
-    $form['role'][$id]['alias'] = array(
-      '#type' => 'textfield',
-      '#default_value' => $role['alias'],
-      '#size' => 50,
-      '#maxlength' => 50,
-    );
-    $form['role'][$id]['weight'] = array(
-      '#type' => 'weight',
-      '#default_value' => $role['weight'],
-      '#delta' => 10,
-    );
-  }
-
-  // Generate fieldsets for each node type.
-  foreach (node_type_get_types() as $type => $bundle) {
-    $form['nodeaccess'][$type] = array(
-      '#type' => 'fieldset',
-      '#collapsible' => TRUE,
-      '#collapsed' => TRUE,
-      '#title' => check_plain($bundle->name),
-      '#tree' => TRUE,
-      '#theme' => 'nodeaccess_admin_form_types',
-    );
-
-    $form['nodeaccess'][$type]['show'] = array(
-      '#type' => 'checkbox',
-      '#title' => t('Show grant tab for this node type'),
-      '#default_value' => isset($show[$type]) ? $show[$type] : 0,
-    );
-
-    // Set default author permissions for node type.
-    $author_prefs = variable_get('nodeaccess_authors', array());
-    $form['nodeaccess'][$type]['author']['grant_view'] = array(
-      '#type' => 'checkbox',
-      '#default_value' => $author_prefs[$type]['grant_view'],
-    );
-    $form['nodeaccess'][$type]['author']['grant_update'] = array(
-      '#type' => 'checkbox',
-      '#default_value' => $author_prefs[$type]['grant_update'],
-    );
-    $form['nodeaccess'][$type]['author']['grant_delete'] = array(
-      '#type' => 'checkbox',
-      '#default_value' => $author_prefs[$type]['grant_delete'],
-    );
-
-    $perms = variable_get('nodeaccess_' . $type, array());
-    foreach ($perms as $perm) {
-      $opts[$perm['gid']] = $perm;
-    }
-
-    // Set default role permissions for node type.
-    foreach (user_roles() as $id => $role) {
-      $form['nodeaccess'][$type]['roles'][$id]['name'] = array('#markup' => $role);
-      $form['nodeaccess'][$type]['roles'][$id]['grant_view'] = array(
-        '#type' => 'checkbox',
-        '#default_value' => isset($opts[$id]['grant_view']) ? $opts[$id]['grant_view'] : 0,
-      );
-      $form['nodeaccess'][$type]['roles'][$id]['grant_update'] = array(
-        '#type' => 'checkbox',
-        '#default_value' => isset($opts[$id]['grant_update']) ? $opts[$id]['grant_update'] : 0,
-      );
-      $form['nodeaccess'][$type]['roles'][$id]['grant_delete'] = array(
-        '#type' => 'checkbox',
-        '#default_value' => isset($opts[$id]['grant_delete']) ? $opts[$id]['grant_delete'] : 0,
-      );
-    }
-
-    // Set the default permissions if userreference exists and is enabled on
-    // the content type.
-    if (module_exists('user_reference')) {
-      $bundle = field_extract_bundle('node', $bundle);
-      $fields = field_info_instances('node', $bundle);
-      $user_reference_perms = variable_get('nodeaccess_' . $type . '_user_reference', array());
-
-      $field_types = field_info_field_types();
-
-      foreach ($fields as $field) {
-        $field = field_info_field($field['field_name']);
-        if ($field['type'] == 'user_reference') {
-          $enabled = isset($user_reference_perms[$field['field_name']]['enabled']) ? $user_reference_perms[$field['field_name']]['enabled'] : 0;
-          $view = isset($user_reference_perms[$field['field_name']]['grant_view']) ? $user_reference_perms[$field['field_name']]['grant_view'] : 0;
-          $update = isset($user_reference_perms[$field['field_name']]['grant_update']) ? $user_reference_perms[$field['field_name']]['grant_update'] : 0;
-          $delete = isset($user_reference_perms[$field['field_name']]['grant_delete']) ? $user_reference_perms[$field['field_name']]['grant_delete'] : 0;
-
-          $form['nodeaccess'][$type]['user_reference'][$field['field_name']]['name'] = array('#value' => t($field_types[$field['type']]['label']));
-          $form['nodeaccess'][$type]['user_reference'][$field['field_name']]['enabled'] = array(
-            '#type' => 'checkbox',
-            '#default_value' => $enabled,
-          );
-          $form['nodeaccess'][$type]['user_reference'][$field['field_name']]['grant_view'] = array(
-            '#type' => 'checkbox',
-            '#default_value' => $view,
-          );
-          $form['nodeaccess'][$type]['user_reference'][$field['field_name']]['grant_update'] = array(
-            '#type' => 'checkbox',
-            '#default_value' => $update,
-          );
-          $form['nodeaccess'][$type]['user_reference'][$field['field_name']]['grant_delete'] = array(
-            '#type' => 'checkbox',
-            '#default_value' => $delete,
-          );
-        }
-      }
-    }
-  }
-  $form['submit'] = array(
-    '#type' => 'submit',
-    '#value' => t('Save Grants'),
-  );
-  return $form;
-}
-
-/**
- * Submit function for nodeaccess_admin_form.
- */
-function nodeaccess_admin_form_submit($form, $form_state) {
-  $form_values = $form_state['values'];
-  // Save priority.
-  variable_set('nodeaccess-priority', $form_values['priority']);
-  // Save preserve.
-  variable_set('nodeaccess-preserve', $form_values['preserve']);
-  // Save allowed grants.
-  foreach ($form_values['grant'] as $id => $val) {
-    $allowed_grants[$id] = $val;
-  }
-  variable_set('nodeaccess-grants', $allowed_grants);
-  // Save allowed roles, role aliases and weights.
-  $alias_prefs = array();
-  $allowed_roles = array();
-  foreach ($form_values['role'] as $id => $val) {
-    $allowed_roles[$id] = $val['allow'];
-    // Save alias and weight only for allowed roles.
-    if ($val['allow']) {
-      // If alias is empty, default to role name.
-      if ($val['alias']) {
-        $alias_prefs[$id]['name'] = $val['alias'];
-      }
-      else {
-        $alias_prefs[$id]['name'] = $val['name'];
-      }
-      $alias_prefs[$id]['weight'] = $val['weight'];
-    }
-    else {
-      // Otherwise, we only save alias if one was specified.
-      if ($val['alias']) {
-        $alias_prefs[$id]['name'] = $val['alias'];
-        $alias_prefs[$id]['weight'] = $val['weight'];
-      }
-    }
-  }
-  variable_set('nodeaccess-roles', $allowed_roles);
-  nodeaccess_save_role_aliases($alias_prefs);
-  // Save author and role permissions for each node type.
-  $author_prefs = array();
-  foreach (node_type_get_types() as $type => $name) {
-    $grants = array();
-    foreach ($form_values[$type]['roles'] as $role => $val) {
-      $grants[] = array(
-        'gid' => $role,
-        'realm' => 'nodeaccess_rid',
-        'grant_view' => $val['grant_view'],
-        'grant_update' => $val['grant_update'],
-        'grant_delete' => $val['grant_delete'],
-      );
-    }
-    variable_set('nodeaccess_' . $type, $grants);
-    if ($form_values[$type]['show']) {
-      $allowed_types[$type] = 1;
-    }
-    else {
-      $allowed_types[$type] = 0;
-    }
-    $author_prefs[$type] = $form_values[$type]['author'];
-    // Also save userreference default permissions if enabled.
-    if (module_exists('user_reference') && isset($form_values[$type]['user_reference'])) {
-      $user_reference_grants = array();
-      foreach ($form_values[$type]['user_reference'] as $user_reference_field => $val) {
-        $user_reference_grants[$user_reference_field] = array(
-          'gid' => 'nodeaccess_uid',
-          'enabled' => $val['enabled'],
-          'grant_view' => $val['grant_view'],
-          'grant_update' => $val['grant_update'],
-          'grant_delete' => $val['grant_delete'],
-        );
-      }
-      variable_set('nodeaccess_' . $type . '_user_reference', $user_reference_grants);
-    }
-  }
-  variable_set('nodeaccess_authors', $author_prefs);
-  // Save allowed node types.
-  variable_set('nodeaccess-types', $allowed_types);
-  node_access_needs_rebuild(TRUE);
-  drupal_set_message(t('Grants saved.'));
-}
-
-/**
- * Theme function for nodeaccess_admin_form.
- */
-function theme_nodeaccess_admin_form_roles($variables) {
-  $output = '';
-  $form = $variables['form'];
-  $rows = array();
-  foreach (element_children($form) as $rid) {
-    // Classify the weight element for TableDrag.
-    $form[$rid]['weight']['#attributes']['class'] = array('roles-order-weight');
-
-    // Mark the table row as draggable for TableDrag.
-    $row = array(
-      'data' => array(),
-      'class' => array('draggable'),
-    );
-    // Render the table columns.
-    $row['data'][] = drupal_render($form[$rid]['allow']);
-    $row['data'][] = drupal_render($form[$rid]['alias']);
-    $row['data'][] = drupal_render($form[$rid]['weight']);
-    $rows[] = $row;
-  }
-
-  $header = array(t('Allow Role'), t('Alias'), t('Weight'));
-  $output .= theme('table', array(
-    'header' => $header,
-    'rows' => $rows,
-    'attributes' => array('id' => 'roles-order'),
-  ));
-  $output .= drupal_render_children($form);
-
-  // Attach TableDrag to the table ID and contained weight elements.
-  drupal_add_tabledrag('roles-order', 'order', 'sibling', 'roles-order-weight');
-
-  return $output;
-}
-
-/**
- * Output the grant table.
- * @todo Please document this function.
- * @see http://drupal.org/node/1354
- */
-function theme_nodeaccess_admin_form_types($variables) {
-  // @todo Number of parameters does not match hook_theme.
-  $form = $variables['form'];
-  $output = drupal_render($form['show']);
-  $roles = element_children($form['roles']);
-  $header = array(t('Role'), t('View'), t('Edit'), t('Delete'));
-  foreach ($roles as $role) {
-    $row = array();
-    $row[] = drupal_render($form['roles'][$role]['name']);
-    $row[] = drupal_render($form['roles'][$role]['grant_view']);
-    $row[] = drupal_render($form['roles'][$role]['grant_update']);
-    $row[] = drupal_render($form['roles'][$role]['grant_delete']);
-    $rows[] = $row;
-  }
-  $output .= theme('table', array('header' => $header, 'rows' => $rows));
-  $header = array(t('Author Settings'), t('View'), t('Edit'), t('Delete'));
-  $row = array();
-  $row[] = t('Node author');
-  $row[] = drupal_render($form['author']['grant_view']);
-  $row[] = drupal_render($form['author']['grant_update']);
-  $row[] = drupal_render($form['author']['grant_delete']);
-
-  $output .= theme('table', array('header' => $header, 'rows' => array($row)));
-
-  $output .= '<small>' . t('The settings selected for the node author will define what permissions the node author has. This cannot be changed on individual node grants.') . '</small>';
-
-  if (module_exists('user_reference') && isset($form['user_reference'])) {
-    $user_reference_fields = element_children($form['user_reference']);
-    $header = array(t('User Reference Field'),
-      t('Enable this field'),
-      t('View'),
-      t('Edit'),
-      t('Delete'),
-    );
-    $rows = array();
-    foreach ($user_reference_fields as $user_reference_field) {
-      $row = array();
-
-      $row[] = $form['user_reference'][$user_reference_field]['name']['#value'];
-      $row[] = drupal_render($form['user_reference'][$user_reference_field]['enabled']);
-      $row[] = drupal_render($form['user_reference'][$user_reference_field]['grant_view']);
-      $row[] = drupal_render($form['user_reference'][$user_reference_field]['grant_update']);
-      $row[] = drupal_render_children($form['user_reference'][$user_reference_field]['grant_delete']);
-      $rows[] = $row;
-    }
-    $output .= theme('table', array(
-        'header' => $header,
-        'rows' => $rows,
-      ));
-    $output .= '<small>' . t('If enabled, the value of the user reference field will be granted the associated permissions. If a user changes the value of the user reference field on a node, the associated user will be modified in the node-specific access table.') . '</small>';
-  }
-  return $output;
-}
 
 /**
  * Menu callback. Draws the grant tab.
+ *
+ * @param Object $node
+ *
+ * @return array
+ *   Form array.
  */
-function nodeaccess_grants($node) {
+function nodeaccess_grant_tab($node) {
   return drupal_get_form('nodeaccess_grants_form', $node);
 }
 
 /**
  * Menu callback. Draws the grant tab.
+ *
+ * @param $form
+ * @param $form_state
+ * @param $node
+ *
+ * @return mixed
  */
 function nodeaccess_grants_form($form, &$form_state, $node) {
-  $form_values = &$form_state['values'];
+  $form_values = $form_state['values'];
+
   if (!$form_values) {
     $form_values = array();
-    // Load all roles.
-    $result = db_query("SELECT r.rid, nra.name, na.grant_view, na.grant_update, na.grant_delete
-      FROM {role} r
-      LEFT JOIN {nodeaccess_role_alias} nra ON r.rid = nra.rid
-      LEFT JOIN {node_access} na ON r.rid = na.gid AND na.realm = :realm AND na.nid = :nid
-      ORDER BY nra.weight, nra.name", array(':realm' => 'nodeaccess_rid', ':nid' => $node->nid));
-    foreach ($result as $grant) {
-      $form_values['rid'][$grant->rid] = array(
-        'name' => $grant->name,
-        'grant_view' => (boolean) $grant->grant_view,
-        'grant_update' => (boolean) $grant->grant_update,
-        'grant_delete' => (boolean) $grant->grant_delete,
-      );
+    $grants = nodeaccess_get_grants($node);
+    $form_values['rid'] = isset($grants['rid']) ? $grants['rid'] : array();
+    $form_values['uid'] = isset($grants['uid']) ? $grants['uid'] : array();
+  }
+  elseif ($form_values['keys']) {
+    $name = preg_replace('!\*+!', '%', $form_values['keys']);
+    $uid = array_keys($form_values['uid']);
+
+    $query = db_select('users', 'u')
+      ->fields('u', array('uid', 'name'))
+      ->condition('name', $name, "LIKE");
+
+    if (isset($form_values['uid']) && count($form_values['uid'])) {
+      $query->condition('uid', $uid, 'NOT IN');
     }
-    // Load users from node_access.
-    $results = db_query("SELECT uid, name, grant_view, grant_update, grant_delete
-      FROM {node_access}
-      LEFT JOIN {users} ON uid = gid
-      WHERE nid = :nid AND realm = :realm
-      ORDER BY name", array(
-        ':nid' => $node->nid,
-        ':realm' => 'nodeaccess_uid',
-      ));
-    foreach ($results as $account) {
+
+    $result = $query->execute();
+    while ($account = $result->fetch()) {
       $form_values['uid'][$account->uid] = array(
         'name' => $account->name,
         'keep' => 1,
-        'grant_view' => $account->grant_view,
-        'grant_update' => $account->grant_update,
-        'grant_delete' => $account->grant_delete,
+        'grant_view' => isset($form_values['rid'][DRUPAL_AUTHENTICATED_RID]['grant_view']) ?
+          $form_values['rid'][DRUPAL_AUTHENTICATED_RID]['grant_view'] : 0,
+        'grant_update' => isset($form_values['rid'][DRUPAL_AUTHENTICATED_RID]['grant_update']) ?
+          $form_values['rid'][DRUPAL_AUTHENTICATED_RID]['grant_update'] : 0,
+        'grant_delete' => isset($form_values['rid'][DRUPAL_AUTHENTICATED_RID]['grant_delete']) ?
+          $form_values['rid'][DRUPAL_AUTHENTICATED_RID]['grant_delete'] : 0,
       );
     }
   }
-  else {
-    // Perform search.
-    if ($form_values['keys']) {
-      // @todo rewrite
-      $params = array();
-      $sql = "SELECT uid, name FROM {users} WHERE name LIKE :name";
-      $name = preg_replace('!\*+!', '%', $form_values['keys']);
-      $params[':name'] = $name;
-      $users = '';
-      if (isset($form_values['uid']) && is_array($form_values['uid'])) {
-        $sql .= ' AND uid NOT IN (:uid)';
-        $users = implode(',', array_keys($form_values['uid']));
-        $params[':uid'] = $users;
-      }
-
-      $result = db_query($sql, $params);
-      foreach ($result as $account) {
 
-        $form_values['uid'][$account->uid] = array(
-          'name' => $account->name,
-          'keep' => 0,
-        );
-      }
-    }
-    // Calculate default grants for found users.
-    // rewrite
-    if (isset($form_values['uid']) && is_array($form_values['uid'])) {
-      foreach (array_keys($form_values['uid']) as $uid) {
-        if (!$form_values['uid'][$uid]['keep']) {
-          foreach (array('grant_view', 'grant_update', 'grant_delete') as $grant_type) {
-            $form_values['uid'][$uid][$grant_type] = db_query_range("SELECT count(*) FROM {node_access} na LEFT JOIN {users_roles} r ON na.gid = r.rid WHERE nid = :nid AND realm = :realm AND uid = :uid AND $grant_type = 1",
-              0, 1,
-              array(
-                ':nid' => $node->nid,
-                ':realm' => 'nodeaccess_rid',
-                ':uid' => $uid,
-              ))->fetchField() ||
-              db_query_range("SELECT count(*) FROM {node_access} na WHERE nid = :nid AND realm = :realm AND gid = :gid AND $grant_type = 1",
-                0, 1,
-                array(
-                  ':nid' => $node->nid,
-                  ':realm' => 'nodeaccess_uid',
-                  ':gid' => $uid,
-                )
-                )->fetchField();
-          }
-          $form_values['uid'][$uid]['keep'] = TRUE;
-        }
-      }
-    }
-  }
   if (!isset($form_values['rid'])) {
     $form_values['rid'] = array();
   }
@@ -665,52 +276,51 @@ function nodeaccess_grants_form($form, &$form_state, $node) {
   // If $preserve is TRUE, the fields the user is not allowed to view or
   // edit are included in the form as hidden fields to preserve them.
   $preserve = variable_get('nodeaccess-preserve', 1);
-
   // Roles table.
   if (is_array($roles)) {
     $form['rid'] = array('#tree' => TRUE);
     foreach ($roles as $key => $field) {
       if (isset($allowed_roles[$key]) && $allowed_roles[$key]) {
-          $form['rid'][$key]['name'] = array(
+        $form['rid'][$key]['name'] = array(
+          '#type' => 'hidden',
+          '#value' => $field['name'],
+        );
+        if ($allowed_grants['view']) {
+          $form['rid'][$key]['grant_view'] = array(
+            '#type' => 'checkbox',
+            '#default_value' => $field['grant_view'],
+          );
+        }
+        elseif ($preserve) {
+          $form['rid'][$key]['grant_view'] = array(
             '#type' => 'hidden',
-            '#value' => $field['name'],
+            '#value' => $field['grant_view'],
           );
-          if ($allowed_grants['view']) {
-            $form['rid'][$key]['grant_view'] = array(
-              '#type' => 'checkbox',
-              '#default_value' => $field['grant_view'],
-            );
-          }
-          elseif ($preserve) {
-            $form['rid'][$key]['grant_view'] = array(
-              '#type' => 'hidden',
-              '#value' => $field['grant_view'],
-            );
-          }
-          if ($allowed_grants['edit']) {
-            $form['rid'][$key]['grant_update'] = array(
-              '#type' => 'checkbox',
-              '#default_value' => $field['grant_update'],
-            );
-          }
-          elseif ($preserve) {
-            $form['rid'][$key]['grant_update'] = array(
-              '#type' => 'hidden',
-              '#value' => $field['grant_update'],
-            );
-          }
-          if ($allowed_grants['delete']) {
-            $form['rid'][$key]['grant_delete'] = array(
-              '#type' => 'checkbox',
-              '#default_value' => $field['grant_delete'],
-            );
-          }
-          elseif ($preserve) {
-            $form['rid'][$key]['grant_delete'] = array(
-              '#type' => 'hidden',
-              '#value' => $field['grant_delete'],
-            );
-          }
+        }
+        if ($allowed_grants['edit']) {
+          $form['rid'][$key]['grant_update'] = array(
+            '#type' => 'checkbox',
+            '#default_value' => $field['grant_update'],
+          );
+        }
+        elseif ($preserve) {
+          $form['rid'][$key]['grant_update'] = array(
+            '#type' => 'hidden',
+            '#value' => $field['grant_update'],
+          );
+        }
+        if ($allowed_grants['delete']) {
+          $form['rid'][$key]['grant_delete'] = array(
+            '#type' => 'checkbox',
+            '#default_value' => $field['grant_delete'],
+          );
+        }
+        elseif ($preserve) {
+          $form['rid'][$key]['grant_delete'] = array(
+            '#type' => 'hidden',
+            '#value' => $field['grant_delete'],
+          );
+        }
       }
       elseif ($preserve) {
         $form['rid'][$key]['name'] = array(
@@ -810,14 +420,20 @@ function nodeaccess_grants_form($form, &$form_state, $node) {
     '#type' => 'submit',
     '#value' => t('Save Grants'),
   );
+
   return $form;
 }
 
+
 /**
  * Validate function for nodeaccess_grants_form.
+ *
+ * @param array $form
+ * @param array &$form_state
  */
-function nodeaccess_grants_form_validate($form, $form_state) {
+function nodeaccess_grants_form_validate($form, &$form_state) {
   $form_values = &$form_state['values'];
+
   // Delete unkept users.
   if (isset($form_values['uid']) && is_array($form_values['uid'])) {
     foreach ($form_values['uid'] as $uid => $row) {
@@ -826,14 +442,17 @@ function nodeaccess_grants_form_validate($form, $form_state) {
       }
     }
   }
+
   if (!isset($form_values['uid'])) {
     unset($form_values['uid']);
   }
-
 }
 
 /**
  * Submit function for nodeaccess_grants_form.
+ *
+ * @param array $form
+ * @param array &$form_state
  */
 function nodeaccess_grants_form_submit($form, &$form_state) {
 
@@ -848,60 +467,50 @@ function nodeaccess_grants_form_submit($form, &$form_state) {
   }
 }
 
+
 /**
  * Private function to submit the per-node grants table.
+ *
+ * @param array $form
+ * @param array &$form_state
  */
-function _nodeaccess_grants_form_submit($form, $form_state) {
+function _nodeaccess_grants_form_submit($form, &$form_state) {
   $form_values = &$form_state['values'];
-  global $user;
+
   $grants = array();
   $nid = $form_values['nid'];
   $node = node_load($nid);
+
   foreach (array('uid', 'rid') as $type) {
     $realm = 'nodeaccess_' . $type;
+
     if (isset($form_values[$type]) && is_array($form_values[$type])) {
       foreach ($form_values[$type] as $gid => $line) {
         $grant = array(
           'gid' => $gid,
           'realm' => $realm,
-          'grant_view' => empty($line['grant_view']) ? 0 : $line['grant_view'],
-          'grant_update' => empty($line['grant_update']) ? 0 : $line['grant_update'],
-          'grant_delete' => empty($line['grant_delete']) ? 0 : $line['grant_delete'],
+          'grant_view' => empty($line['grant_view']) ? 0 : 1,
+          'grant_update' => empty($line['grant_update']) ? 0 : 1,
+          'grant_delete' => empty($line['grant_delete']) ? 0 : 1,
         );
-        if ($grant['grant_view'] || $grant['grant_update'] || $grant['grant_delete']) {
-          $grants[] = $grant;
-        }
+        $grants[] = $grant;
       }
     }
-    node_access_write_grants($node, $grants, $realm);
-  }
-
-  // Save role and user grants to our own table.
-  db_delete('nodeaccess')
-  ->condition('nid', $nid)
-  ->execute();
-  foreach ($grants as $grant) {
-    $id = db_insert('nodeaccess')
-        ->fields(array(
-          'nid' => $nid,
-          'gid' => $grant['gid'],
-          'realm' => $grant['realm'],
-          'grant_view' => $grant['grant_view'],
-          'grant_update' => $grant['grant_update'],
-          'grant_delete' => $grant['grant_delete'],
-        ))
-    ->execute();
   }
+  nodeaccess_set_grants($node, $grants);
 }
 
 /**
  * Theme function for nodeaccess_grants_form.
+ *
+ * @param array $vars
+ *
+ * @return string
  */
-function theme_nodeaccess_grants_form($variables) {
+function theme_nodeaccess_grants_form($vars) {
   $output = '';
-  // @todo: Number of parameters in this theme function does not match number
-  // of parameters found in hook_theme.
-  $form = $variables['form'];
+
+  $form = $vars['form'];
   $rows = array();
   $allowed_roles = variable_get('nodeaccess-roles', array());
   $allowed_grants = variable_get('nodeaccess-grants', array());
@@ -918,28 +527,37 @@ function theme_nodeaccess_grants_form($variables) {
 
   // Roles table.
   $roles = element_children($form['rid']);
+
   if (count($roles) && count($allowed_roles)) {
     $header = array();
     $header[] = t('Role');
+
     if ($allowed_grants['view']) {
       $header[] = t('View');
     }
+
     if ($allowed_grants['edit']) {
       $header[] = t('Edit');
     }
+
     if ($allowed_grants['delete']) {
       $header[] = t('Delete');
     }
+
     foreach ($roles as $key) {
+
       if (isset($allowed_roles[$key]) && $allowed_roles[$key]) {
         $row = array();
         $row[] = $role_names[$key] . drupal_render($form['rid'][$key]['name']);
+
         if ($allowed_grants['view']) {
           $row[] = drupal_render($form['rid'][$key]['grant_view']);
         }
+
         if ($allowed_grants['edit']) {
           $row[] = drupal_render($form['rid'][$key]['grant_update']);
         }
+
         if ($allowed_grants['delete']) {
           $row[] = drupal_render($form['rid'][$key]['grant_delete']);
         }
@@ -960,6 +578,7 @@ function theme_nodeaccess_grants_form($variables) {
   // Users table.
   unset($rows);
   $users = element_children($form['uid']);
+
   if (count($users) > 0) {
     $header = array();
     $rows = array();
@@ -997,8 +616,14 @@ function theme_nodeaccess_grants_form($variables) {
   return $output;
 }
 
+
 /**
  * Implements hook_node_grants().
+ *
+ * @param Object $account
+ * @parap string $op
+ *
+ * @return array
  */
 function nodeaccess_node_grants($account, $op) {
   $roles = is_array($account->roles) ? array_keys($account->roles) : array(-1);
@@ -1011,17 +636,10 @@ function nodeaccess_node_grants($account, $op) {
 
 /**
  * Implements hook_node_update().
+ *
+ * @param Object $node
  */
 function nodeaccess_node_update($node) {
-  // Node author may have changed, overwrite old record.
-  $author_prefs = variable_get('nodeaccess_authors', array());
-  // Array is prepopulated with grant values.
-  $grant = $author_prefs[$node->type];
-  $grant['gid'] = $node->uid;
-  $grant['realm'] = 'nodeaccess_author';
-  $grants = array();
-  $grants[] = $grant;
-  node_access_write_grants($node, $grants, 'nodeaccess_author');
   if (module_exists('user_reference')) {
     $fields = variable_get('nodeaccess_' . $node->type . '_user_reference', array());
     foreach (array_keys($fields) as $field_name) {
@@ -1041,25 +659,26 @@ function nodeaccess_node_update($node) {
 
 /**
  * Implements hook_node_delete().
+ *
+ * @param Object $node
  */
 function nodeaccess_node_delete($node) {
   // Deleting node, delete related permissions.
-  db_delete('nodeaccess')
-  ->condition('nid', $node->nid)
-  ->execute();
-  foreach (array('uid', 'rid', 'author') as $type) {
-    $realm = 'nodeaccess_' . $type;
-    node_access_write_grants($node, array(), $realm);
-  }
+  nodeaccess_delete_grants($node);
 }
 
 /**
  * Implements hook_node_access_records().
+ *
+ * @param Object $node
+ *
+ * @return array|NULL
  */
 function nodeaccess_node_access_records($node) {
-  if (nodeaccess_disabling()) {
-    return;
+  if (nodeaccess_disabling() || !$node->status) {
+    return NULL;
   }
+
   // Need to find out if node has own grants or whether to use defaults.
   $default = variable_get('nodeaccess_' . $node->type, array());
 
@@ -1073,51 +692,25 @@ function nodeaccess_node_access_records($node) {
     'priority' => variable_get('nodeaccess-priority', 0),
   );
 
-  $result = db_query("SELECT count(*) FROM {nodeaccess} WHERE nid = :nid", array(':nid' => $node->nid))->fetchField();
-  if ($result < 1) {
-    // Node has no own grants, use defaults if published.
-    if ($node->status) {
-      $grants = $default;
-    }
-    else {
-      // Otherwise, check access to unpublished content for authenticated and
-      // anonymous users.
-      $role_perms = user_role_permissions(array(
-        DRUPAL_ANONYMOUS_RID => DRUPAL_ANONYMOUS_RID,
-        DRUPAL_AUTHENTICATED_RID => DRUPAL_AUTHENTICATED_RID,
-      ));
-      // Anonymous user setting.
-      $grants = array();
-      $grants[] = array(
-        'gid' => DRUPAL_ANONYMOUS_RID,
-        'grant_view' => isset($role_perms[DRUPAL_ANONYMOUS_RID]['bypass node access']) ?
-          intval($role_perms[DRUPAL_ANONYMOUS_RID]['bypass node access']) : 0,
-      );
-      // Authenticated user setting.
-      $grants[] = array(
-        'gid' => DRUPAL_AUTHENTICATED_RID,
-        'grant_view' => isset($role_perms[DRUPAL_AUTHENTICATED_RID]['bypass node access']) ?
-          intval($role_perms[DRUPAL_AUTHENTICATED_RID]['bypass node access']) : 0,
-      );
-    }
+  $query = db_select('nodeaccess', 'n')
+    ->fields('n', array('gid', 'realm', 'grant_view', 'grant_update', 'grant_delete'))
+    ->condition('nid', $node->nid, '=');
+  $result = $query->execute();
+  if (!$result->rowCount()) {
+    // Node has no own grants, use defaults.
+    $grants = $default;
   }
   else {
     // Node has own grants, use them.
-    $result = db_query("SELECT nid, gid, realm, grant_view, grant_update, grant_delete FROM {nodeaccess} WHERE nid = :nid", array(':nid' => $node->nid));
     $grants = array();
-    foreach ($result as $row) {
-      $grants[] = array(
-        'gid' => $row->gid,
-        'realm' => $row->realm,
-        'grant_view' => $row->grant_view,
-        'grant_update' => $row->grant_update,
-        'grant_delete' => $row->grant_delete,
-      );
+    while ($row = $result->fetchAssoc()) {
+      $grants[] = $row;
     }
   }
+
   // Apply author grants.
   $author_prefs = variable_get('nodeaccess_authors', array());
-  // Array is prepopulated with grant values.
+  // Array is pre-populated with grant values.
   $grant = $author_prefs[$node->type];
   $grant['gid'] = $node->uid;
   $grant['realm'] = 'nodeaccess_author';
@@ -1129,9 +722,11 @@ function nodeaccess_node_access_records($node) {
     // Merge missing default grant keys.
     $grants[$id] = $grants[$id] + $grant_defaults;
   }
+
   return $grants;
 }
 
+
 /**
  * Mark module to be in process of disabling.
  *
@@ -1139,17 +734,24 @@ function nodeaccess_node_access_records($node) {
  *
  * @param mixed $set
  *   A boolean set or NULL to not change status.
+ *
+ * @return bool
  */
 function nodeaccess_disabling($set = NULL) {
   static $disabling = FALSE;
+
   if ($set !== NULL) {
     $disabling = $set;
   }
+
   return $disabling;
 }
 
+
 /**
  * Implements hook_node_type_delete().
+ *
+ * @param Object $info
  */
 function nodeaccess_node_type_delete($info) {
   // Node type is being deleted, delete its preferences.
@@ -1159,8 +761,11 @@ function nodeaccess_node_type_delete($info) {
   variable_set('nodeaccess_authors', $author_prefs);
 }
 
+
 /**
  * Implements hook_node_type_update().
+ *
+ * @param Object $info
  */
 function nodeaccess_node_type_update($info) {
   // Node type has changed, move preferences to new type.
@@ -1179,55 +784,67 @@ function nodeaccess_node_type_update($info) {
   }
 }
 
+
 /**
  * Implements hook_node_type_insert().
+ *
+ * @param Object $info
  */
 function nodeaccess_node_type_insert($info) {
   // New node type, default to whatever is set for access content permission.
   $role_perms = user_role_permissions(array(1 => 1, 2 => 2));
-  $role_perms[1]['access content'] = isset($role_perms[1]['access content'])?
+  $role_perms[DRUPAL_ANONYMOUS_RID]['access content'] = isset($role_perms[1]['access content']) ?
                                       intval($role_perms[1]['access content']) : 0;
-  $role_perms[2]['access content'] = isset($role_perms[2]['access content'])?
+  $role_perms[DRUPAL_AUTHENTICATED_RID]['access content'] = isset($role_perms[2]['access content']) ?
                                       intval($role_perms[2]['access content']) : 0;
   $grants[] = array(
-    'gid' => 1,
+    'gid' => DRUPAL_ANONYMOUS_RID,
     'realm' => 'nodeaccess_rid',
-    'grant_view' => $role_perms[1]['access content'],
+    'grant_view' => $role_perms[DRUPAL_ANONYMOUS_RID]['access content'],
     'grant_update' => 0,
     'grant_delete' => 0,
   );
   $grants[] = array(
-    'gid' => 2,
+    'gid' => DRUPAL_AUTHENTICATED_RID,
     'realm' => 'nodeaccess_rid',
-    'grant_view' => $role_perms[1]['access content'],
+    'grant_view' => $role_perms[DRUPAL_AUTHENTICATED_RID]['access content'],
     'grant_update' => 0,
     'grant_delete' => 0,
   );
   variable_set('nodeaccess_' . $info->type, $grants);
-  // Add all permissions for author.
+
+  // Add permissions for author.
   $author_prefs = variable_get('nodeaccess_authors', array());
   $author_prefs[$info->type] = array(
     'grant_view' => 0,
     'grant_update' => 0,
     'grant_delete' => 0,
   );
+
   variable_set('nodeaccess_authors', $author_prefs);
   node_access_needs_rebuild(TRUE);
 }
 
+
 /**
  * Retrieve role aliases.
  *
- * @return array or role aliases indexed by rid.
+ * @return array
+ *  Role aliases indexed by rid.
  */
 function nodeaccess_get_role_aliases() {
   $aliases = array();
-  $sql = 'SELECT r.rid, r.name, a.name AS alias, a.weight
-    FROM {role} r
-    LEFT JOIN {nodeaccess_role_alias} a ON r.rid = a.rid
-    ORDER BY a.weight, r.weight, a.name, r.name';
-  $results = db_query($sql);
-  foreach ($results as $a) {
+  $query = db_select('role', 'r');
+  $query->join('nodeaccess_role_alias', 'a', 'r.rid = a.rid');
+  $query->fields('r', array('rid', 'name'));
+  $query->addField('a', 'name', 'alias');
+  $query->addField('a', 'weight');
+  $query->orderBy('a.weight');
+  $query->orderBy('r.weight');
+  $query->orderBy('a.name');
+  $query->orderBy('r.name');
+  $result = $query->execute();
+  while($a = $result->fetch()) {
     $aliases[$a->rid]['name'] = $a->name;
     $aliases[$a->rid]['alias'] = $a->alias;
     $aliases[$a->rid]['weight'] = $a->weight;
@@ -1235,7 +852,6 @@ function nodeaccess_get_role_aliases() {
   return $aliases;
 }
 
-
 /**
  * Save a role alias.
  *
@@ -1253,15 +869,16 @@ function nodeaccess_save_role_aliases($edit) {
       ))->execute();
     }
   }
-
   return;
 }
 
 /**
  * Insert userreference grants from a node.
+ *
+ * @param Object $node
  */
 function nodeaccess_insert_user_reference($node) {
-  $form_values = _nodeaccess_get_grants($node);
+  $form_values = nodeaccess_get_grants($node);
   // Now, append or overwrite the uid with what was specified in the user
   // reference field.
   $fields = variable_get('nodeaccess_' . $node->type . '_user_reference', array());
@@ -1289,9 +906,11 @@ function nodeaccess_insert_user_reference($node) {
 
 /**
  * Delete all userreference user grants from a node.
+ *
+ * @param Object $node
  */
 function nodeaccess_delete_user_reference($node) {
-  $form_values = _nodeaccess_get_grants($node);
+  $form_values = nodeaccess_get_grants($node);
   // Now, append or overwrite the uid with what was specified in the user
   // reference field.
   $fields = variable_get('nodeaccess_' . $node->type . '_user_reference', array());
@@ -1304,45 +923,198 @@ function nodeaccess_delete_user_reference($node) {
   }
   $form_values['nid'] = $node->nid;
   $form_state = array('values' => $form_values);
+
   _nodeaccess_grants_form_submit(NULL, $form_state);
 }
 
 /**
  * Return the grants applied to a node object used for Grant form.
  *
+ * @param Object $node
+ *
  * @return array
  *   An array of grants with keys 'rid' for roles and 'uid' for users.
  */
-function _nodeaccess_get_grants($node) {
+function nodeaccess_get_grants($node) {
   $grants = array();
-  // Load all roles.
-  $result = db_query("SELECT r.rid, nra.name, na.grant_view, na.grant_update, na.grant_delete FROM {role} r LEFT JOIN {nodeaccess_role_alias} nra ON r.rid = nra.rid LEFT JOIN {node_access} na ON r.rid = na.gid AND na.realm = :realm AND na.nid = :nid ORDER BY nra.weight, nra.name",
-    array(
-      ':realm' => 'nodeaccess_rid',
-      ':nid' => $node->nid,
-    ));
-  foreach ($result as $grant) {
-    $grants['rid'][$grant->rid] = array(
-      'name' => $grant->name,
-      'grant_view' => (boolean) $grant->grant_view,
-      'grant_update' => (boolean) $grant->grant_update,
-      'grant_delete' => (boolean) $grant->grant_delete,
-    );
+
+  // Load grants by roles.
+  $query = db_select('nodeaccess', 'na');
+  $query->join('role', 'r', 'r.rid = na.gid');
+  $query->join('nodeaccess_role_alias', 'nra', 'nra.rid = r.rid');
+  $query->fields('r', array('rid', 'name'));
+  $query->fields('na', array('grant_view', 'grant_update', 'grant_delete'));
+  $query->condition('na.realm', 'nodeaccess_rid', '=');
+  $query->condition('na.nid', $node->nid, '=');
+  $query->orderBy('nra.weight');
+  $query->orderBy('nra.name');
+  $result = $query->execute();
+
+  while ($grant = $result->fetch()) {
+    if ($grant->name) {
+      $grants['rid'][$grant->rid] = array(
+        'name' => $grant->name,
+        'grant_view' => (bool) $grant->grant_view,
+        'grant_update' => (bool) $grant->grant_update,
+        'grant_delete' => (bool) $grant->grant_delete,
+      );
+    }
   }
-  // Load users from node_access.
-  $result = db_query("SELECT uid, name, grant_view, grant_update, grant_delete FROM {node_access} LEFT JOIN {users} ON uid = gid WHERE nid = :nid AND realm = :realm ORDER BY name",
-    array(
-      ':nid' => $node->nid,
-      ':realm' => 'nodeaccess_uid',
-    ));
-  foreach ($result as $account) {
+  // Load grants by users
+  $query = db_select('node_access', 'na');
+  $query->join('users', 'u', 'u.uid = na.gid');
+  $query->fields('u', array('uid', 'name'));
+  $query->fields('na', array('grant_view', 'grant_update', 'grant_delete'));
+  $query->condition('na.nid', $node->nid, '=');
+  $query->condition('na.realm', 'nodeaccess_uid', '=');
+  $query->orderBy('name');
+  $result = $query->execute();
+
+  while ($account = $result->fetch()) {
     $grants['uid'][$account->uid] = array(
       'name' => $account->name,
-      'keep' => 1,
-      'grant_view' => $account->grant_view,
-      'grant_update' => $account->grant_update,
-      'grant_delete' => $account->grant_delete,
+      'keep' => TRUE,
+      'grant_view' => (bool) $account->grant_view,
+      'grant_update' => (bool) $account->grant_update,
+      'grant_delete' => (bool) $account->grant_delete,
     );
   }
   return $grants;
 }
+
+/**
+ * Set all grants for a node to nodeaccess table and acquire them.
+ *
+ * @param $node
+ *   node object the grants are being applied to.
+ * @param array $grants
+ *   array of grants as defined (@see nodeaccess_save_grant).
+ */
+function nodeaccess_set_grants($node, $grants = array()) {
+  // Allow other modules to edit all grants for array.
+  drupal_alter('nodeaccess_grants', $grants, $node);
+
+  nodeaccess_delete_grants($node);
+
+  if (count($grants)) {
+    foreach ($grants as $grant) {
+      // Allow other modules to edit single grants.
+      drupal_alter('nodeaccess_grant', $grant, $node);
+      nodeaccess_save_grant($node, $grant);
+    }
+  }
+
+  node_access_acquire_grants($node);
+}
+
+
+/**
+ * Delete all grants from nodeaccess table for this node.
+ *
+ * @param $node
+ *   node object whose grants are being revoked.
+ */
+function nodeaccess_delete_grants($node) {
+  try {
+    db_delete('nodeaccess')
+      ->condition('nid', $node->nid)
+      ->execute();
+
+    node_access_acquire_grants($node);
+  }
+  catch (Exception $e) {
+    drupal_set_message(t("Database error has occurred while clearing nodeaccess table."), 'error');
+    watchdog('nodeaccess', 'Database error: @message.', array('@message' => $e->getMessage()), WATCHDOG_ERROR);
+  }
+}
+
+/**
+ * Revoke all custom grants from nodeaccess table for this node.  Essentially
+ * reset the grants to their default state (by node type).
+ *
+ * @param $node
+ *   node object whose grants are being revoked.
+ */
+function nodeaccess_revoke_grants($node) {
+  nodeaccess_delete_grants($node);
+  node_access_acquire_grants($node);
+}
+
+/**
+ * Save the grant settings/options for the node.
+ *
+ * @param $node
+ *   node object the grant is being applied to.
+ * @param array $grant
+ *   array(
+ *    'gid' => (int) gid for realm,
+ *    'realm' => (string) what realm the access grant belongs to (ex: nodeaccess_rid).
+ *    'grant_view' => (int) view access being granted,
+ *    'grant_update' => (int) update access being granted,
+ *    'grant_delete' => (int) delete access being granted,
+ *  )
+ */
+function nodeaccess_save_grant($node, $grant) {
+  // Save role and user grants to our own table.
+  try {
+    db_insert('nodeaccess')
+      ->fields(array(
+        'nid' => $node->nid,
+        'gid' => $grant['gid'],
+        'realm' => $grant['realm'],
+        'grant_view' => (int) $grant['grant_view'],
+        'grant_update' => (int) $grant['grant_update'],
+        'grant_delete' => (int) $grant['grant_delete'],
+      ))
+      ->execute();
+  }
+  catch (Exception $e) {
+    drupal_set_message(t("Database error has occurred while saving to nodeaccess table."), 'error');
+    watchdog('nodeaccess', 'Database error: @message.', array('@message' => $e->getMessage()), WATCHDOG_ERROR);
+  }
+}
+
+/**
+ * Add the grant tab to the specified node type.
+ *
+ * @param string $type
+ *   The node type we are adding the grant tab for.
+ */
+function nodeaccess_add_type_grant($type) {
+  $grants = variable_get('nodeaccess-types', array());
+  $grants[$type] = TRUE;
+
+  nodeaccess_set_type_grants($grants);
+}
+
+/**
+ * Delete the grant tab for specified node type.
+ *
+ * @param string $type
+ *   The node type we are removing the grant tab from.
+ */
+function nodeaccess_delete_type_grant($type) {
+  $grants = variable_get('nodeaccess-types', array());
+
+  if (isset($grants[$type])) {
+    unset($grants[$type]);
+  }
+
+  nodeaccess_set_type_grants($grants);
+}
+
+/**
+ * Set the grant tab settings for all node types.
+ *
+ * @param array $types
+ *   The types that will have the grant tab appear.
+ *
+ */
+function nodeaccess_set_type_grants($types = array()) {
+  if (!is_array($types)) {
+    return;
+  }
+
+  variable_set('nodeaccess-types', $types);
+  node_access_needs_rebuild(TRUE);
+}
diff --git a/drupal7/sites/all/modules/nodeaccess/tests/nodeaccess_grant.test b/drupal7/sites/all/modules/nodeaccess/tests/nodeaccess_grant.test
new file mode 100644
index 00000000..f4b04a2f
--- /dev/null
+++ b/drupal7/sites/all/modules/nodeaccess/tests/nodeaccess_grant.test
@@ -0,0 +1,62 @@
+<?php
+
+/**
+ * @file
+ * Tests for the nodeaccess module.
+ */
+
+/**
+ * Tests the functionality of the nodeaccess module.
+ */
+class NodeaccesssGrantTabTestCase extends DrupalWebTestCase {
+
+  /**
+   * Enable the nodeaccess module.
+   */
+  public function setUp() {
+    parent::setUp('nodeaccess');
+  }
+
+  /**
+   * Provide some information about this test case.
+   */
+  public static function getInfo() {
+    return array(
+      'name' => 'Nodeaccess Grant Tab visibility',
+      'description' => 'Tests whether the Grant tab appears correctly..',
+      'group' => 'Nodeaccess',
+    );
+  }
+
+  /**
+   * Make sure the 'Grant' tab is available from the node view when appropriate.
+   * @test
+   */
+  public function testGrantTabVisbility() {
+    // Create a test user, and a node with nodeaccess enabled for its type.
+    $user = $this->drupalCreateUser(array('grant node permissions'));
+    $node = $this->drupalCreateNode();
+    nodeaccess_add_type_grant($node->type);
+
+    // First, before logging in, we shouldn't see a link.
+    $this->drupalGet("node/{$node->nid}");
+    $this->assertNoLinkByHref("node/{$node->nid}/grant");
+
+    // Log in and the grant tab should appear.
+    $this->drupalLogin($user);
+    $this->drupalGet("node/{$node->nid}");
+    $this->assertLinkByHref("node/{$node->nid}/grant");
+
+    // Disable nodeaccess for this content type, and the tab should be gone.
+    nodeaccess_delete_type_grant($node->type);
+    $this->drupalGet("node/{$node->nid}");
+    $this->assertNoLinkByHref("node/{$node->nid}/grant");
+
+    // Confirm that the anonymous user still doesn't see the grant tab.
+    $this->drupalLogout();
+    $this->drupalGet("node/{$node->nid}");
+    $this->assertNoLinkByHref("node/{$node->nid}/grant");
+
+  }
+
+}
diff --git a/drupal7/sites/all/modules/nodeaccess/tests/nodeaccess_publish.test b/drupal7/sites/all/modules/nodeaccess/tests/nodeaccess_publish.test
new file mode 100644
index 00000000..c6048d03
--- /dev/null
+++ b/drupal7/sites/all/modules/nodeaccess/tests/nodeaccess_publish.test
@@ -0,0 +1,178 @@
+<?php
+
+/**
+ * @file
+ * Tests for the nodeaccess module.
+ */
+
+/**
+ * Tests the functionality of the nodeaccess module.
+ */
+class NodeaccesssPublishTestCase extends DrupalWebTestCase {
+
+  // Nodes.
+  protected $authored_node;
+  protected $published_node;
+  protected $unpublished_node;
+
+  // Users.
+  protected $admin_user;
+  protected $author_user;
+  protected $basic_user;
+
+  // Default Grant.
+  protected $grant;
+
+  /**
+   * Enable the nodeaccess module. Add type grant for pages. Rebuild perms.
+   */
+  public function setUp() {
+    parent::setUp('nodeaccess');
+
+    user_role_grant_permissions(DRUPAL_ANONYMOUS_RID, array('access content'));
+
+    nodeaccess_add_type_grant('page');
+
+    // Let's create a sample authenticated user with basic permissions.
+    $this->basic_user = $this->drupalCreateUser(array('access content'));
+
+    // Let's create a sample author-type user.
+    $this->author_user = $this->drupalCreateUser(array('access content', 'view own unpublished content'));
+
+    // Let's create a more admin-type user.
+    $this->admin_user = $this->drupalCreateUser(array('access content', 'bypass node access'));
+
+    // Create a published page node, authored by admin.
+    $this->published_node = $this->drupalCreateNode(array('type' => 'page', 'uid' => 1));
+
+    // Create an unpublished page node, authored by admin.
+    $this->unpublished_node = $this->drupalCreateNode(array('type' => 'page', 'uid' => 1, 'status' => 0));
+
+    // Create an unpublished page node, authored by author.
+    $this->authored_node = $this->drupalCreateNode(array('type' => 'page', 'uid' => $this->author_user->uid, 'status' => 0));
+
+    $this->grant = array(
+      array(
+        'gid' => DRUPAL_ANONYMOUS_RID,
+        'realm' => 'nodeaccess_rid',
+        'grant_view' => 1,
+        'grant_update' => 0,
+        'grant_delete' => 0,
+      ),
+      array(
+        'gid' => DRUPAL_AUTHENTICATED_RID,
+        'realm' => 'nodeaccess_rid',
+        'grant_view' => 1,
+        'grant_update' => 0,
+        'grant_delete' => 0,
+      ),
+    );
+
+    node_access_rebuild();
+  }
+
+  /**
+   * Provide some information about this test case.
+   */
+  public static function getInfo() {
+    return array(
+      'name' => 'Nodeaccess node status based tests.',
+      'description' => 'Tests nodes are correctly hidden based on node published status.',
+      'group' => 'Nodeaccess',
+    );
+  }
+
+  /**
+   * Test standard, published node visibility.
+   */
+  public function testPublishedView() {
+
+    nodeaccess_set_grants($this->published_node, $this->grant);
+
+    // Baseline, should be visible by all users, including anon.
+    $this->drupalGet("node/{$this->published_node->nid}");
+    $this->assertResponse(200, 'Anonymous user is allowed to view the content.');
+
+    // Authenticated User
+    $this->drupalLogin($this->basic_user);
+    $this->drupalGet("node/{$this->published_node->nid}");
+    $this->assertResponse(200, 'Authenticated user is allowed to view the content.');
+    $this->drupalLogout();
+
+    // Author User
+    $this->drupalLogin($this->author_user);
+    $this->drupalGet("node/{$this->published_node->nid}");
+    $this->assertResponse(200, 'Author user is allowed to view the content.');
+    $this->drupalLogout();
+
+    // Admin User
+    $this->drupalLogin($this->admin_user);
+    $this->drupalGet("node/{$this->published_node->nid}");
+    $this->assertResponse(200, 'Admin user is allowed to view the content.');
+    $this->drupalLogout();
+
+  }
+
+  /**
+   * Test unpublished node visibility.
+   */
+  public function testUnpublishedView() {
+    nodeaccess_set_grants($this->unpublished_node, $this->grant);
+
+    // Anonymous users should NOT see the content.
+    $this->drupalGet("node/{$this->unpublished_node->nid}");
+    $this->assertResponse(403, 'Anonymous user is not allowed to view the unpublished content.');
+
+    // Authenticated User, should NOT see content.
+    $this->drupalLogin($this->basic_user);
+    $this->drupalGet("node/{$this->unpublished_node->nid}");
+    $this->assertResponse(403, 'Authenticated user is not allowed to view the unpublished content.');
+    $this->drupalLogout();
+
+    // Author User, should NOT see the content, not author of this node.
+    $this->drupalLogin($this->author_user);
+    $this->drupalGet("node/{$this->unpublished_node->nid}");
+    $this->assertResponse(403, 'Author user is not allowed to view the unpublished content.');
+    $this->drupalLogout();
+
+    // Admin User should see content, has administer content permission.
+    $this->drupalLogin($this->admin_user);
+    $this->drupalGet("node/{$this->unpublished_node->nid}");
+    $this->assertResponse(200, 'Admin user is allowed to view the unpublished content (bypass).');
+    $this->drupalLogout();
+
+  }
+
+/**
+   * Test unpublished node visibility.
+   */
+  public function testAuthoredView() {
+    nodeaccess_set_grants($this->authored_node, $this->grant);
+
+    // Anonymous users should NOT see the content.
+    $this->drupalGet("node/{$this->authored_node->nid}");
+    $this->assertResponse(403, 'Anonymous user is not allowed to view the unpublished content.');
+
+    // Authenticated User, should NOT see content.
+    $this->drupalLogin($this->basic_user);
+    $this->drupalGet("node/{$this->authored_node->nid}");
+    $this->assertResponse(403, 'Authenticated user is not allowed to view the unpublished content.');
+    $this->drupalLogout();
+
+    // Author User, should NOT see the content, not author of this node.
+    $this->drupalLogin($this->author_user);
+    $this->drupalGet("node/{$this->authored_node->nid}");
+    $this->assertResponse(200, 'Author user is allowed to view their own unpublished content.');
+    $this->drupalLogout();
+
+    // Admin User should see content, has administer content permission.
+    $this->drupalLogin($this->admin_user);
+    $this->drupalGet("node/{$this->authored_node->nid}");
+    $this->assertResponse(200, 'Admin user is allowed to view the unpublished content (bypass).');
+    $this->drupalLogout();
+
+  }
+
+
+
+}
diff --git a/drupal7/sites/all/modules/nodeaccess/tests/nodeaccess_role.test b/drupal7/sites/all/modules/nodeaccess/tests/nodeaccess_role.test
new file mode 100644
index 00000000..26b24fd2
--- /dev/null
+++ b/drupal7/sites/all/modules/nodeaccess/tests/nodeaccess_role.test
@@ -0,0 +1,236 @@
+<?php
+
+/**
+ * @file
+ * Tests for the nodeaccess module.
+ */
+
+/**
+ * Tests the functionality of the nodeaccess module.
+ */
+class NodeaccesssRoleTestCase extends DrupalWebTestCase {
+
+  protected $node;
+
+  protected $user;
+
+  /**
+   * Enable the nodeaccess module. Add type grant for pages. Rebuild perms.
+   */
+  public function setUp() {
+    parent::setUp('nodeaccess');
+
+    user_role_grant_permissions(DRUPAL_ANONYMOUS_RID, array('access content'));
+
+    nodeaccess_add_type_grant('page');
+
+    // Create a page node, authored by admin.
+    $this->node = $this->drupalCreateNode(array('type' => 'page', 'uid' => 1));
+
+    // Let's create a sample authenticated user with basic permissions.
+    $this->user = $this->drupalCreateUser(array('access content'));
+
+    node_access_rebuild();
+  }
+
+  /**
+   * Provide some information about this test case.
+   */
+  public static function getInfo() {
+    return array(
+      'name' => 'Nodeaccess Role-based node visibility',
+      'description' => 'Tests nodes are correctly hidden based on user roles.',
+      'group' => 'Nodeaccess',
+    );
+  }
+
+  /**
+   * Test node access when there should be no access whatsoever.
+   */
+  public function testRoleNodeVisibilityNoAccess() {
+    $grants = array(
+      array(
+        'gid' => DRUPAL_ANONYMOUS_RID,
+        'realm' => 'nodeaccess_rid',
+        'grant_view' => 0,
+        'grant_update' => 0,
+        'grant_delete' => 0,
+      ),
+      array(
+        'gid' => DRUPAL_AUTHENTICATED_RID,
+        'realm' => 'nodeaccess_rid',
+        'grant_view' => 0,
+        'grant_update' => 0,
+        'grant_delete' => 0,
+      ),
+    );
+
+    $this->checkRoleGrantAccesses($grants);
+
+  }
+
+  /**
+   * Test node access when only Authenticated users should have view access.
+   */
+  public function testRoleNodeVisibilityViewAuthenticatedOnly() {
+    $grants = array(
+      array(
+        'gid' => DRUPAL_ANONYMOUS_RID,
+        'realm' => 'nodeaccess_rid',
+        'grant_view' => 0,
+        'grant_update' => 0,
+        'grant_delete' => 0,
+      ),
+      array(
+        'gid' => DRUPAL_AUTHENTICATED_RID,
+        'realm' => 'nodeaccess_rid',
+        'grant_view' => 1,
+        'grant_update' => 0,
+        'grant_delete' => 0,
+      ),
+    );
+
+    nodeaccess_set_grants($this->node, $grants);
+
+    // Run all tests from grant.
+    $this->checkRoleGrantAccesses($grants);
+
+  }
+
+  /**
+   * Test node access when authenticated only should have view/edit perms.
+   */
+  public function testRoleNodeVisibilityViewEditAuthenticatedOnly() {
+    $grants = array(
+      array(
+        'gid' => DRUPAL_ANONYMOUS_RID,
+        'realm' => 'nodeaccess_rid',
+        'grant_view' => 0,
+        'grant_update' => 0,
+        'grant_delete' => 0,
+      ),
+      array(
+        'gid' => DRUPAL_AUTHENTICATED_RID,
+        'realm' => 'nodeaccess_rid',
+        'grant_view' => 1,
+        'grant_update' => 1,
+        'grant_delete' => 0,
+      ),
+    );
+
+    nodeaccess_set_grants($this->node, $grants);
+
+    // Run all tests from grant.
+    $this->checkRoleGrantAccesses($grants);
+
+  }
+
+  /**
+   * Test node access when authenticated only should have full access.
+   */
+  public function testRoleNodeVisibilityAllAuthenticatedOnly() {
+    $grants = array(
+      array(
+        'gid' => DRUPAL_ANONYMOUS_RID,
+        'realm' => 'nodeaccess_rid',
+        'grant_view' => 0,
+        'grant_update' => 0,
+        'grant_delete' => 0,
+      ),
+      array(
+        'gid' => DRUPAL_AUTHENTICATED_RID,
+        'realm' => 'nodeaccess_rid',
+        'grant_view' => 1,
+        'grant_update' => 1,
+        'grant_delete' => 1,
+      ),
+    );
+
+    nodeaccess_set_grants($this->node, $grants);
+
+    // Run all tests from grant.
+    $this->checkRoleGrantAccesses($grants);
+
+  }
+
+  /**
+   * Test node access when anon and authenticated users should have full access.
+   */
+  public function testRoleNodeVisibilityFullAccess() {
+    $grants = array(
+      array(
+        'gid' => DRUPAL_ANONYMOUS_RID,
+        'realm' => 'nodeaccess_rid',
+        'grant_view' => 1,
+        'grant_update' => 1,
+        'grant_delete' => 1,
+      ),
+      array(
+        'gid' => DRUPAL_AUTHENTICATED_RID,
+        'realm' => 'nodeaccess_rid',
+        'grant_view' => 1,
+        'grant_update' => 1,
+        'grant_delete' => 1,
+      ),
+    );
+
+    nodeaccess_set_grants($this->node, $grants);
+
+    // Run all tests from grant.
+    $this->checkRoleGrantAccesses($grants);
+
+
+  }
+
+  /**
+   * This method will check the access for the node based on grants supplied.
+   *
+   * @param array $grants
+   *  Array of role grants to check.
+   */
+  protected function checkRoleGrantAccesses($grants = array()) {
+    // Anonymous user should not be able to access this regular node.
+
+    foreach ($grants as $grant) {
+      $logout = FALSE;
+      if (!empty($grant['realm']) && $grant['realm'] == 'nodeaccess_rid') {
+        if (isset($grant['gid']) && $grant['gid'] == DRUPAL_AUTHENTICATED_RID) {
+          $this->drupalLogin($this->user);
+          $logout = TRUE;
+        }
+
+        // Check View Access
+        $this->drupalGet("node/{$this->node->nid}");
+        if (!empty($grant['grant_view']) && $grant['grant_view']) {
+          $this->assertResponse(200, 'User is allowed to view the content.');
+        }
+        else {
+          $this->assertResponse(403, 'User is not allowed to view the content.');
+        }
+
+        // Check Edit Access
+        $this->drupalGet("node/{$this->node->nid}/edit");
+        if (!empty($grant['grant_update']) && $grant['grant_update']) {
+          $this->assertResponse(200, 'User is allowed to edit the content.');
+        }
+        else {
+          $this->assertResponse(403, 'User is not allowed to edit the content.');
+        }
+
+        // Check Delete Access
+        $this->drupalGet("node/{$this->node->nid}/delete");
+        if (!empty($grant['grant_delete']) && $grant['grant_delete']) {
+          $this->assertResponse(200, 'User is allowed to delete the content.');
+        }
+        else {
+          $this->assertResponse(403, 'User is not allowed to delete the content.');
+        }
+
+        if ($logout) {
+          $this->drupalLogout();
+        }
+      }
+    }
+  }
+
+}
