diff --git a/tests/php/test_montage_save_missing_layout.php b/tests/php/test_montage_save_missing_layout.php new file mode 100644 index 0000000000..2699748c06 --- /dev/null +++ b/tests/php/test_montage_save_missing_layout.php @@ -0,0 +1,77 @@ +id = (int)$id; + $this->name = $GLOBALS["existing_layouts"][(int)$id]; + $this->uid = 1; + } + } + public static function find_one($p = array()) { return null; } + public function Id() { return $this->id; } + public function Name($n = null) { if ($n !== null) $this->name = $n; return $this->name; } + public function UserId($u = null) { if ($u !== null) $this->uid = $u; return $this->uid; } + public function Positions($p = null) { return $p; } + public function save() { $GLOBALS["saved_layouts"][] = $this->name; $this->id = $this->id ?: 99; return true; } + } +}'); +class FakeUser { public function Id() { return 1; } } +function canEdit($x) { return false; } +function validCardinal($i) { return is_scalar($i) ? preg_replace('/\D/', '', $i) : ''; } +function zm_session_start() { return true; } +function zm_setcookie($name, $value, $options = array()) { $GLOBALS['setcookies'][] = array($name, $value); return true; } + +function run_save($request) { + global $root; + $src = file_get_contents($root.'/web/includes/actions/montage.php'); + $src = preg_replace('/^<\?php/', '', $src); + $src = preg_replace('/\?>\s*$/', '', $src); + $src = preg_replace('/^require_once\(.*$/m', '', $src); + $GLOBALS['warnings'] = $GLOBALS['saved_layouts'] = $GLOBALS['setcookies'] = array(); + $_SESSION = array(); + $_REQUEST = $request; + $fn = function ($action) use ($src) { + global $error_message, $redirect, $user; + $user = new FakeUser(); + $error_message = ''; $redirect = null; + try { eval($src); } catch (\Throwable $e) { return get_class($e).': '.$e->getMessage(); } + return null; + }; + return $fn('Save'); +} + +$existing_layouts = array(20 => 'Mine'); + +$base = array('object' => 'MontageLayout', 'Name' => '', 'Positions' => '{}'); + +$ex = run_save($base + array('zmMontageLayout' => '20')); +check('empty name saves over the selected existing layout', $ex === null && $saved_layouts === array('Mine')); + +$ex = run_save($base + array('zmMontageLayout' => '77')); +check('empty name with a deleted layout id saves nothing', $saved_layouts === array()); +check('empty name with a deleted layout id does not select a new layout', $setcookies === array()); + +echo "$tests tests, $failures failures\n"; +exit($failures ? 1 : 0); diff --git a/web/includes/actions/montage.php b/web/includes/actions/montage.php index 1a2994b350..7b48362094 100644 --- a/web/includes/actions/montage.php +++ b/web/includes/actions/montage.php @@ -47,6 +47,11 @@ if ( isset($_REQUEST['object']) ) { } } else { $Layout = new ZM\MontageLayout(validCardinal($_REQUEST['zmMontageLayout'])); + # The selected layout is gone (deleted from another tab); don't create an unnamed one + if ( !$Layout->Id() ) { + ZM\Warning('Layout Id=' . validCardinal($_REQUEST['zmMontageLayout']) . ' not found for save'); + return; + } } if (canEdit('System') or !$Layout->Id() or ($user->Id() == $Layout->UserId())) { $Layout->UserId($user->Id());