diff --git a/CHANGELOG.md b/CHANGELOG.md index d4909a7..b46b191 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ installation guidance and source-checked behavior. --- 3.0 --- +* issue#292: Use the `'monitor'` gettext text domain for the `monitor_list` setting labels in `setup.php` instead of `'thold'`, pass it to the Criticality label in `setup.php` that had no domain, add a test that every translation helper call passes the plugin text domain, refresh `locales/po/cacti.pot` to match current PHP source strings, and remove orphan `locales/LC_MESSAGES/ka.mo` * security: Require POST for the save, remove, mute and card-order actions so csrf-magic covers them, and only let a dashboard's owner save over it * security: Bind the plugin version lookup and the device_remove cleanup deletes as prepared statements instead of building them from the device list * bug: Escape the device description, location, site name and Monitor Text in the List view diff --git a/locales/LC_MESSAGES/ka.mo b/locales/LC_MESSAGES/ka.mo deleted file mode 100644 index 9c3640a..0000000 Binary files a/locales/LC_MESSAGES/ka.mo and /dev/null differ diff --git a/locales/po/cacti.pot b/locales/po/cacti.pot index a5a0285..8d419ff 100644 --- a/locales/po/cacti.pot +++ b/locales/po/cacti.pot @@ -850,8 +850,11 @@ msgid "Any" msgstr "" #: setup.php -#, php-format -msgid "Click 'Continue' to %s monitoring on these Device(s)" +msgid "Click 'Continue' to enable monitoring on these Device(s)" +msgstr "" + +#: setup.php +msgid "Click 'Continue' to disable monitoring on these Device(s)" msgstr "" #: setup.php diff --git a/setup.php b/setup.php index 5b42b3c..9a90123 100644 --- a/setup.php +++ b/setup.php @@ -162,7 +162,7 @@ function monitor_device_table_bottom() { ]; if (version_compare(CACTI_VERSION, '1.3.0', '<')) { - $select = '' . __('Criticality') . ''; foreach ($criticalities as $index => $crit) { if ($index == get_request_var('criticality')) { @@ -939,8 +939,8 @@ function monitor_config_settings() { 'max_length' => '255' ], 'monitor_list' => [ - 'friendly_name' => __('Notification List', 'thold'), - 'description' => __('Select a Notification List below. All Emails subscribed to the notification list will be notified.', 'thold'), + 'friendly_name' => __('Notification List', 'monitor'), + 'description' => __('Select a Notification List below. All Emails subscribed to the notification list will be notified.', 'monitor'), 'method' => 'drop_sql', 'sql' => 'SELECT id, name FROM plugin_notification_lists ORDER BY name', 'default' => '', diff --git a/tests/Unit/I18nTextDomainTest.php b/tests/Unit/I18nTextDomainTest.php new file mode 100644 index 0000000..f0d61ee --- /dev/null +++ b/tests/Unit/I18nTextDomainTest.php @@ -0,0 +1,64 @@ + number of calls. They are listed so the check passes without + * widening the change that introduced it. The count must match exactly: + * fixing a call without lowering its count here fails the test, so the + * list can only shrink. Do not add entries for new code. + */ + $known = [ + // nav bar and list labels with no domain (one is the core 'Devices' label) + 'includes/render.php' => 2, + // report e-mail paragraph + 'poller_functions.php' => 1, + // core role name passed to auth_augment_roles() + 'setup.php' => 1, + ]; + + $found = []; + + foreach (i18n_domain_offenders(dirname(__DIR__, 2), 'monitor') as $offender) { + $found[$offender['file']][] = $offender['file'] . ':' . $offender['line'] . ' ' . $offender['call'] . ': ' . $offender['reason']; + } + + $unexpected = []; + + foreach ($found as $file => $lines) { + if (count($lines) !== ($known[$file] ?? 0)) { + $unexpected = array_merge($unexpected, $lines); + } + } + + foreach ($known as $file => $count) { + if (!isset($found[$file])) { + $unexpected[] = $file . ': expected ' . $count . ' known calls without the text domain, found none; remove it from the list'; + } + } + + expect($unexpected)->toBe([]); +}); diff --git a/tests/Unit/MonitorConfigSettingsTest.php b/tests/Unit/MonitorConfigSettingsTest.php new file mode 100644 index 0000000..ebf4f38 --- /dev/null +++ b/tests/Unit/MonitorConfigSettingsTest.php @@ -0,0 +1,40 @@ + '1 Minute'); + $settings_user = array(); + $tabs_graphs = array(); + + monitor_config_settings(); + + expect($tabs)->toHaveKey('monitor'); + expect($settings['monitor'])->toHaveKey('monitor_list'); + expect($settings['monitor']['monitor_list']['friendly_name'])->toBe('Notification List'); + expect($settings['monitor']['monitor_list']['description'])->toContain('Notification List'); +}); diff --git a/tests/bin/check-i18n-domain.php b/tests/bin/check-i18n-domain.php new file mode 100644 index 0000000..be3c710 --- /dev/null +++ b/tests/bin/check-i18n-domain.php @@ -0,0 +1,223 @@ + + * + * Exits 1 and prints file:line for each offender, 0 when there are none. + */ + +if (!function_exists('i18n_domain_offenders')) { + /** + * @param string $root Plugin root directory. + * @param string $domain Text domain every helper call must pass. + * + * @return array + */ + function i18n_domain_offenders($root, $domain) { + $root = rtrim(realpath($root), '/'); + $offenders = []; + + foreach (i18n_domain_files($root) as $file) { + $relative = substr($file, strlen($root) + 1); + + foreach (i18n_domain_check_source(file_get_contents($file), $domain) as $offender) { + $offender['file'] = $relative; + $offenders[] = $offender; + } + } + + return $offenders; + } + + /** + * @param string $root Plugin root directory. + * + * @return array Absolute paths, sorted. + */ + function i18n_domain_files($root) { + $skip = ['vendor', 'tests', 'locales']; + $files = []; + + $iterator = new RecursiveIteratorIterator( + new RecursiveCallbackFilterIterator( + new RecursiveDirectoryIterator($root, FilesystemIterator::SKIP_DOTS), + function ($current) use ($root, $skip) { + $relative = substr($current->getPathname(), strlen($root) + 1); + + if ($current->getFilename()[0] === '.') { + return false; + } + + if ($current->isDir()) { + return !in_array($relative, $skip, true); + } + + return $current->getExtension() === 'php'; + } + ) + ); + + foreach ($iterator as $file) { + $files[] = $file->getPathname(); + } + + sort($files); + + return $files; + } + + /** + * @param string $source PHP source code. + * @param string $domain Expected text domain. + * + * @return array + */ + function i18n_domain_check_source($source, $domain) { + // Position of the domain argument: an index from the start, or -1 for the last argument. + $positions = [ + '__' => -1, '__esc' => -1, '__x' => -1, '__esc_x' => -1, + '__n' => 3, '__esc_n' => 3, '__xn' => 4, '__esc_xn' => 4, + '__gettext' => 1, + ]; + + $tokens = array_values(array_filter( + token_get_all($source), + function ($token) { + return !is_array($token) || !in_array($token[0], [T_WHITESPACE, T_COMMENT, T_DOC_COMMENT], true); + } + )); + $offenders = []; + $count = count($tokens); + + for ($i = 0; $i < $count; $i++) { + if (!is_array($tokens[$i]) || !in_array($tokens[$i][0], [T_STRING, T_NAME_FULLY_QUALIFIED], true)) { + continue; + } + + $name = ltrim($tokens[$i][1], '\\'); + + if (!isset($positions[$name]) || ($tokens[$i + 1] ?? null) !== '(') { + continue; + } + + // Skip method calls, static calls, declarations and instantiations. + $previous = $tokens[$i - 1] ?? null; + + if (is_array($previous) && in_array($previous[0], [T_OBJECT_OPERATOR, T_NULLSAFE_OBJECT_OPERATOR, T_DOUBLE_COLON, T_FUNCTION, T_NEW], true)) { + continue; + } + + $arguments = i18n_domain_arguments($tokens, $i + 1); + $wanted = $positions[$name] === -1 ? count($arguments) - 1 : $positions[$name]; + $reason = null; + + if (!isset($arguments[$wanted]) || ($positions[$name] === -1 && count($arguments) < 2)) { + $reason = 'no text domain argument'; + } elseif (count($arguments[$wanted]) !== 1 || !is_array($arguments[$wanted][0]) || $arguments[$wanted][0][0] !== T_CONSTANT_ENCAPSED_STRING) { + $reason = 'text domain is not a string literal'; + } elseif (substr($arguments[$wanted][0][1], 1, -1) !== $domain) { + $reason = 'text domain is ' . $arguments[$wanted][0][1] . ", expected '" . $domain . "'"; + } + + if ($reason !== null) { + $offenders[] = ['line' => $tokens[$i][2], 'call' => $name . '()', 'reason' => $reason]; + } + } + + return $offenders; + } + + /** + * Split the arguments of the call whose opening parenthesis is at $open. + * + * @param array $tokens Significant tokens. + * @param int $open Index of the '(' token. + * + * @return array> Tokens of each top-level argument. + */ + function i18n_domain_arguments($tokens, $open) { + $arguments = [[]]; + $depth = 0; + $count = count($tokens); + + for ($i = $open; $i < $count; $i++) { + $token = $tokens[$i]; + $text = is_array($token) ? $token[1] : $token; + + if ($text === '(' || $text === '[' || $text === '{' || (is_array($token) && in_array($token[0], [T_CURLY_OPEN, T_DOLLAR_OPEN_CURLY_BRACES], true))) { + $depth++; + + if ($depth === 1) { + continue; + } + } elseif ($text === ')' || $text === ']' || $text === '}') { + $depth--; + + if ($depth === 0) { + break; + } + } elseif ($text === ',' && $depth === 1) { + $arguments[] = []; + + continue; + } + + $arguments[count($arguments) - 1][] = $token; + } + + // A trailing comma leaves an empty last argument. + if (end($arguments) === []) { + array_pop($arguments); + } + + return $arguments; + } +} + +if (PHP_SAPI === 'cli' && isset($argv[0]) && realpath($argv[0]) === __FILE__) { + $domain = $argv[1] ?? ''; + + if ($domain === '') { + fwrite(STDERR, "Usage: php tests/bin/check-i18n-domain.php \n"); + + exit(2); + } + + $offenders = i18n_domain_offenders(dirname(__DIR__, 2), $domain); + + foreach ($offenders as $offender) { + print $offender['file'] . ':' . $offender['line'] . ' ' . $offender['call'] . ': ' . $offender['reason'] . "\n"; + } + + exit($offenders === [] ? 0 : 1); +} diff --git a/tests/bootstrap-unit.php b/tests/bootstrap-unit.php index 8e343fb..d3aceae 100644 --- a/tests/bootstrap-unit.php +++ b/tests/bootstrap-unit.php @@ -101,6 +101,12 @@ function get_selected_theme() { } } +if (!function_exists('get_current_page')) { + function get_current_page() { + return $GLOBALS['__test_current_page'] ?? ''; + } +} + if (!function_exists('monitor_test_mock_db')) { function monitor_test_mock_db($fn, $match, $result) { $GLOBALS['__test_db_fixtures'][] = array('fn' => $fn, 'match' => $match, 'result' => $result);