|
| 1 | +<?php |
| 2 | +/* |
| 3 | + * Copyright 2020 New Relic Corporation. All rights reserved. |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +/*DESCRIPTION |
| 8 | + * Tests a mock of Drupals new hook invoking methods, introduced in Drupal 9.4+ |
| 9 | +*/ |
| 10 | + |
| 11 | +/*SKIPIF |
| 12 | +<?php |
| 13 | +if (version_compare(PHP_VERSION, '8.0', '<')) { |
| 14 | + die("skip: PHP < 8.0 not OAPI\n"); |
| 15 | +} |
| 16 | +*/ |
| 17 | + |
| 18 | +/*INI |
| 19 | +newrelic.framework = drupal8 |
| 20 | +*/ |
| 21 | + |
| 22 | +/*EXPECT |
| 23 | +a1 |
| 24 | +b1 |
| 25 | +b2 |
| 26 | +a3 |
| 27 | +b4 |
| 28 | +b4 |
| 29 | +b2 |
| 30 | +*/ |
| 31 | + |
| 32 | +/*EXPECT_METRICS |
| 33 | +[ |
| 34 | + "?? agent run id", |
| 35 | + "?? timeframe start", |
| 36 | + "?? timeframe stop", |
| 37 | + [ |
| 38 | + [{"name":"DurationByCaller/Unknown/Unknown/Unknown/Unknown/all"}, [1, "??", "??", "??", "??", "??"]], |
| 39 | + [{"name":"DurationByCaller/Unknown/Unknown/Unknown/Unknown/allOther"}, [1, "??", "??", "??", "??", "??"]], |
| 40 | + [{"name":"Framework/Drupal/Hook/hook_1"}, [2, "??", "??", "??", "??", "??"]], |
| 41 | + [{"name":"Framework/Drupal/Hook/hook_2"}, [2, "??", "??", "??", "??", "??"]], |
| 42 | + [{"name":"Framework/Drupal/Hook/hook_3"}, [1, "??", "??", "??", "??", "??"]], |
| 43 | + [{"name":"Framework/Drupal/Hook/hook_4"}, [2, "??", "??", "??", "??", "??"]], |
| 44 | + [{"name":"Framework/Drupal/Module/module_a"}, [2, "??", "??", "??", "??", "??"]], |
| 45 | + [{"name":"Framework/Drupal/Module/module_b"}, [5, "??", "??", "??", "??", "??"]], |
| 46 | + [{"name":"OtherTransaction/all"}, [1, "??", "??", "??", "??", "??"]], |
| 47 | + [{"name":"OtherTransaction/php__FILE__"}, [1, "??", "??", "??", "??", "??"]], |
| 48 | + [{"name":"OtherTransactionTotalTime"}, [1, "??", "??", "??", "??", "??"]], |
| 49 | + [{"name":"OtherTransactionTotalTime/php__FILE__"}, [1, "??", "??", "??", "??", "??"]], |
| 50 | + [{"name":"Supportability/framework/Drupal8/forced"}, [1, "??", "??", "??", "??", "??"]], |
| 51 | + [{"name":"Supportability/Logging/Forwarding/PHP/enabled"}, [1, "??", "??", "??", "??", "??"]], |
| 52 | + [{"name":"Supportability/Logging/Metrics/PHP/enabled"}, [1, "??", "??", "??", "??", "??"]], |
| 53 | + [{"name":"Supportability/api/add_custom_tracer"}, [2, "??", "??", "??", "??", "??"]], |
| 54 | + [{"name":"Custom/invoke_callback_instrumented"}, [1, "??", "??", "??", "??", "??"]], |
| 55 | + [{"name":"Custom/invoke_callback"}, [1, "??", "??", "??", "??", "??"]], |
| 56 | + [{"name":"Custom/invoke_callback_instrumented", |
| 57 | + "scope":"OtherTransaction/php__FILE__"}, [1, "??", "??", "??", "??", "??"]], |
| 58 | + [{"name":"Custom/invoke_callback", |
| 59 | + "scope":"OtherTransaction/php__FILE__"}, [1, "??", "??", "??", "??", "??"]], |
| 60 | + [{"name":"Supportability/Logging/LocalDecorating/PHP/disabled"}, [1, "??", "??", "??", "??", "??"]], |
| 61 | + [{"name":"Supportability/Logging/Labels/PHP/disabled"}, [1, "??", "??", "??", "??", "??"]] |
| 62 | + ] |
| 63 | +] |
| 64 | +*/ |
| 65 | + |
| 66 | + |
| 67 | +require_once __DIR__.'/mock_module_handler.php'; |
| 68 | +require_once __DIR__.'/mock_page_cache_get.php'; |
| 69 | + |
| 70 | +// This specific API is needed for us to instrument the ModuleHandler |
| 71 | +class Drupal { |
| 72 | + public function moduleHandler() { |
| 73 | + return new Drupal\Core\Extension\ModuleHandler(); |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +function module_a_hook_1() { |
| 78 | + echo "a1\n"; |
| 79 | +} |
| 80 | +function module_a_hook_3() { |
| 81 | + echo "a3\n"; |
| 82 | +} |
| 83 | +function module_b_hook_1() { |
| 84 | + echo "b1\n"; |
| 85 | +} |
| 86 | +function module_b_hook_2() { |
| 87 | + echo "b2\n"; |
| 88 | +} |
| 89 | +function module_b_hook_4() { |
| 90 | + echo "b4\n"; |
| 91 | +} |
| 92 | + |
| 93 | +/* Various methods to pass to invokeAllWith */ |
| 94 | +function invoke_callback(callable $hook, string $module) { |
| 95 | + $hook(); |
| 96 | +} |
| 97 | +function invoke_callback_instrumented(callable $hook, string $module) { |
| 98 | + $hook(); |
| 99 | +} |
| 100 | +newrelic_add_custom_tracer("invoke_callback_instrumented"); |
| 101 | + |
| 102 | +class Invoker { |
| 103 | + public function invoke(callable $hook, string $module) { |
| 104 | + $hook(); |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +// Begin Tests ////////////////////////////////////////////////////// |
| 109 | + |
| 110 | +// Create module handler |
| 111 | +$drupal = new Drupal(); |
| 112 | +$handler = $drupal->moduleHandler(); |
| 113 | + |
| 114 | +// Test lambda calback |
| 115 | +$handler->invokeAllWith("hook_1", function (callable $hook, string $module) { |
| 116 | + $hook(); |
| 117 | +}); |
| 118 | + |
| 119 | +// Test string and reference callback |
| 120 | +$func_name = "invoke_callback"; |
| 121 | +$func_name_ref =& $func_name; |
| 122 | +$handler->invokeAllWith("hook_2", $func_name_ref); |
| 123 | + |
| 124 | +//Test callable array callback |
| 125 | +$invoker = new Invoker(); |
| 126 | +$handler->invokeAllWith("hook_3", [$invoker, "invoke"]); |
| 127 | + |
| 128 | +// test callable array callback; function already special instrumented |
| 129 | +// but wraprec not installed yet by observer API because the function |
| 130 | +// has not been called yet - drupal's hook instrumentation will be |
| 131 | +// installed |
| 132 | +$page_cache = new Drupal\page_cache\StackMiddleware\PageCache; |
| 133 | +$handler->invokeallwith("hook_4", [$page_cache, "get"]); |
| 134 | + |
| 135 | +// test string callback; function already instrumented |
| 136 | +// This will reuse the existing wraprec and successfully |
| 137 | +// add instrumentation because the "before" callback is unset |
| 138 | +$func_name = "invoke_callback_instrumented"; |
| 139 | +$handler->invokeallwith("hook_4", $func_name); |
| 140 | + |
| 141 | +// test non-transiently wrapping an already transiently instrumented function |
| 142 | +// This will overwrite the existing transient wrapper |
| 143 | +$func_name = "invoke_callback"; |
| 144 | +newrelic_add_custom_tracer($func_name); |
| 145 | +// Now this test will function the same as above: adding special instrumentation |
| 146 | +// to an already existing wrapper |
| 147 | +$handler->invokeallwith("hook_2", $func_name); |
0 commit comments