Commit d1b36c55 authored by Jakob P's avatar Jakob P
Browse files

Issue #3145431 by japerry: Remove PHP Filter test and deprecate use of PHP Filter.

parent 841b0a66
Loading
Loading
Loading
Loading
+6 −2
Original line number Diff line number Diff line
@@ -18,7 +18,11 @@
    "drupal/core": "^8.8.6|^9.0"
  },
  "require-dev": {
    "drupal/token": "~1.0",
    "drupal/php": "~1.0"
    "drupal/token": "^1.7"
  },
  "extra": {
    "branch-alias": {
      "dev-8.x-2.x": "2.x-dev"
    }
  }
}
+10 −0
Original line number Diff line number Diff line
@@ -58,6 +58,16 @@ function google_analytics_requirements($phase) {
        'value' => t('Debugging enabled'),
      ];
    }

    // Raise warning if php code is being used.
    if ($config->get('visibility.request_path_mode') && $config->get('visibility.request_path_mode') === '2') {
      $requirements['google_analytics_php'] = [
        'title' => t('Google Analytics module'),
        'description' => t('Using PHP code in Google Analytics is deprecated and not available in Drupal 9. You must move your logic into a custom module, and change the <a href=":url">Page Visibility settings</a> to suppress this message.', [':url' => Url::fromRoute('google_analytics.admin_settings_form')->toString()]),
        'severity' => REQUIREMENT_ERROR,
        'value' => t('PHP code exists'),
      ];
    }
  }

  return $requirements;
+11 −7
Original line number Diff line number Diff line
@@ -119,8 +119,10 @@ function google_analytics_page_attachments(array &$page) {
      // Add debugging code.
      if ($debug) {
        $page['#attached']['library'][] = 'google_analytics/google_analytics.debug';
        // phpcs:disable
        // Add the JS test in development to the page.
        // $page['#attached']['library'][] = 'google_analytics/google_analytics.test';
        // phpcs:enable
      }
      else {
        $page['#attached']['library'][] = 'google_analytics/google_analytics';
@@ -457,15 +459,17 @@ function google_analytics_preprocess_item_list__search_results(&$variables) {

  // Only run on search results list.
  if ($config->get('track.site_search')) {
    // There is no search result $variable available that hold the number of
    // items found. The variable $variables['items'] has the current page items
    // only. But the pager item mumber can tell the number of search results.
    global $pager_total_items;

    // Get the pager manager to give us the number of items returned.
    /** @var \Drupal\Core\Pager\PagerManagerInterface $pager_manager */
    $pager_manager = \Drupal::service('pager.manager');
    $items = 0;
    if ($pager_manager->getPager()) {
      $items = $pager_manager->getPager()->getTotalItems();
    }
    $variables['#attached']['html_head'][] = [
      [
        '#tag' => 'script',
        '#value' => 'window.google_analytics_search_results = ' . intval($pager_total_items[0]) . ';',
        '#value' => 'window.google_analytics_search_results = ' . $items . ';',
        '#weight' => JS_LIBRARY - 1,
      ],
      'google_analytics_search_script',
+1 −1
Original line number Diff line number Diff line
@@ -190,7 +190,7 @@ class GoogleAnalyticsAdminSettingsForm extends ConfigFormBase {
      $description = $this->t("Specify pages by using their paths. Enter one path per line. The '*' character is a wildcard. Example paths are %blog for the blog page and %blog-wildcard for every personal blog. %front is the front page.", ['%blog' => '/blog', '%blog-wildcard' => '/blog/*', '%front' => '<front>']);

      if ($this->moduleHandler->moduleExists('php') && $php_access) {
        $options[] = $this->t('Pages on which this PHP code returns <code>TRUE</code> (experts only)');
        $options[] = $this->t('Pages on which this PHP code returns <code>TRUE</code> (not supported in Drupal 9, experts only)');
        $title = $this->t('Pages or PHP code');
        $description .= ' ' . $this->t('If the PHP option is chosen, enter PHP code between %php. Note that executing incorrect PHP code can break your Drupal site.', ['%php' => '<?php ?>']);
      }
+0 −106
Original line number Diff line number Diff line
<?php

namespace Drupal\Tests\google_analytics\Functional;

use Drupal\Component\Utility\Html;
use Drupal\Tests\BrowserTestBase;

/**
 * Test php filter functionality of Google Analytics module.
 *
 * @group Google Analytics
 *
 * @dependencies php
 */
class GoogleAnalyticsPhpFilterTest extends BrowserTestBase {

  /**
   * Modules to enable.
   *
   * @var array
   */
  public static $modules = ['google_analytics', 'php'];

  /**
   * {@inheritdoc}
   */
  protected $defaultTheme = 'stark';

  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    parent::setUp();

    // Administrator with all permissions.
    $permissions_admin_user = [
      'access administration pages',
      'administer google analytics',
      'use PHP for google analytics tracking visibility',
    ];
    $this->admin_user = $this->drupalCreateUser($permissions_admin_user);

    // Administrator who cannot configure tracking visibility with PHP.
    $permissions_delegated_admin_user = [
      'access administration pages',
      'administer google analytics',
    ];
    $this->delegated_admin_user = $this->drupalCreateUser($permissions_delegated_admin_user);
  }

  /**
   * Tests if PHP module integration works.
   */
  public function testGoogleAnalyticsPhpFilter() {
    $ua_code = 'UA-123456-1';
    $this->drupalLogin($this->admin_user);

    $edit = [];
    $edit['google_analytics_account'] = $ua_code;
    $edit['google_analytics_visibility_request_path_mode'] = 2;
    $edit['google_analytics_visibility_request_path_pages'] = '<?php return 0; ?>';
    $this->drupalPostForm('admin/config/system/google-analytics', $edit, t('Save configuration'));

    // Compare saved setting with posted setting.
    $google_analytics_pages = \Drupal::config('google_analytics.settings')->get('visibility.request_path_pages');
    $this->assertEqual('<?php return 0; ?>', $google_analytics_pages, '[testGoogleAnalyticsPhpFilter]: PHP code snippet is intact.');

    // Check tracking code visibility.
    $this->config('google_analytics.settings')->set('visibility.request_path_pages', '<?php return TRUE; ?>')->save();
    $this->drupalGet('');
    $this->assertSession()->responseContains('https://www.google-analytics.com/analytics.js');
    $this->drupalGet('admin');
    $this->assertSession()->responseContains('https://www.google-analytics.com/analytics.js');

    $this->config('google_analytics.settings')->set('visibility.request_path_pages', '<?php return FALSE; ?>')->save();
    $this->drupalGet('');
    $this->assertSession()->responseNotContains('https://www.google-analytics.com/analytics.js');

    // Test administration form.
    $this->config('google_analytics.settings')->set('visibility.request_path_pages', '<?php return TRUE; ?>')->save();
    $this->drupalGet('admin/config/system/google-analytics');
    $this->assertSession()->responseContains(t('Pages on which this PHP code returns <code>TRUE</code> (experts only)'));
    $this->assertSession()->responseContains(Html::escape('<?php return TRUE; ?>'));

    // Login the delegated user and check if fields are visible.
    $this->drupalLogin($this->delegated_admin_user);
    $this->drupalGet('admin/config/system/google-analytics');
    $this->assertSession()->responseNotContains(t('Pages on which this PHP code returns <code>TRUE</code> (experts only)'));
    $this->assertSession()->responseNotContains(Html::escape('<?php return TRUE; ?>'));

    // Set a different value and verify that this is still the same after the
    // post.
    $this->config('google_analytics.settings')->set('visibility.request_path_pages', '<?php return 0; ?>')->save();

    $edit = [];
    $edit['google_analytics_account'] = $ua_code;
    $this->drupalPostForm('admin/config/system/google-analytics', $edit, t('Save configuration'));

    // Compare saved setting with posted setting.
    $google_analytics_visibility_pages = \Drupal::config('google_analytics.settings')->get('visibility.request_path_mode');
    $google_analytics_pages = \Drupal::config('google_analytics.settings')->get('visibility.request_path_pages');
    $this->assertEqual(2, $google_analytics_visibility_pages, '[testGoogleAnalyticsPhpFilter]: Pages on which this PHP code returns TRUE is selected.');
    $this->assertEqual('<?php return 0; ?>', $google_analytics_pages, '[testGoogleAnalyticsPhpFilter]: PHP code snippet is intact.');
  }

}
Loading