diff --git a/classes/helpers/FrmAppHelper.php b/classes/helpers/FrmAppHelper.php index fa9234fec0..b090246702 100644 --- a/classes/helpers/FrmAppHelper.php +++ b/classes/helpers/FrmAppHelper.php @@ -2781,23 +2781,21 @@ public static function get_unique_key( $name, $table_name, $column, $id = 0, $nu $key = self::generate_new_key( $num_chars ); } - $key = self::prevent_numeric_and_reserved_keys( $key ); - - $similar_keys = FrmDb::get_col( - $table_name, - array( - $column . ' like%' => $key, - 'ID !' => $id, - ), - $column - ); + $key = self::prevent_numeric_and_reserved_keys( $key ); + $similar_keys = self::get_similar_keys( $key, $table_name, $column, $id ); // Create a unique field id if it has already been used. if ( ! in_array( $key, $similar_keys, true ) ) { return $key; } - $key = self::maybe_truncate_key_before_appending( $column, $key ); + $truncated_key = self::maybe_truncate_key_before_appending( $column, $key ); + + if ( $truncated_key !== $key ) { + // Keys already created from the truncated key aren't in the list loaded for the full key. + $key = $truncated_key; + $similar_keys = self::get_similar_keys( $key, $table_name, $column, $id ); + } /** * Allow for a custom separator between the attempted key and the generated suffix. @@ -2818,6 +2816,25 @@ public static function get_unique_key( $name, $table_name, $column, $id = 0, $nu return $key_check; } + /** + * @param string $key + * @param string $table_name + * @param string $column + * @param int $id + * + * @return array + */ + private static function get_similar_keys( $key, $table_name, $column, $id ) { + return FrmDb::get_col( + $table_name, + array( + $column . ' like%' => $key, + 'ID !' => $id, + ), + $column + ); + } + /** * Avoid trying to append to a really long key, * The database limit is 100 for form and field keys so we want to avoid getting too close. diff --git a/tests/phpunit/misc/test_FrmAppHelper.php b/tests/phpunit/misc/test_FrmAppHelper.php index 84d0864862..c9f77110ec 100644 --- a/tests/phpunit/misc/test_FrmAppHelper.php +++ b/tests/phpunit/misc/test_FrmAppHelper.php @@ -522,6 +522,22 @@ public function test_get_unique_key() { $this->assertNotSame( $super_long_form_key, $unique_key ); } + public function test_get_unique_key_increments_for_truncated_long_key() { + $table_name = 'frm_forms'; + $column = 'form_key'; + $long_key = str_repeat( 'a', 75 ); + $truncated = substr( $long_key, 0, 60 ); + + $this->factory->form->create( array( 'form_key' => $long_key ) ); + + $first = FrmAppHelper::get_unique_key( $long_key, $table_name, $column ); + $this->assertSame( $truncated . '2', $first ); + $this->factory->form->create( array( 'form_key' => $first ) ); + + $second = FrmAppHelper::get_unique_key( $long_key, $table_name, $column ); + $this->assertSame( $truncated . '3', $second, 'The suffix should skip keys already created from the truncated key.' ); + } + private function add_field_to_form( $form_id, $field_key ) { $type = 'text'; $this->factory->field->create( compact( 'type', 'form_id', 'field_key' ) );