Categories: You can create duplicate categories with same VelVarName
Our current logic for creating categories verify that, depending on the Category Name being parsed as a Velocity Var Name, we should not have more than one category with the same VelVarName.
https://github.com/dotCMS/core/blob/master/dotCMS/src/main/java/com/dotmarketing/portlets/categories/action/EditCategoryAction.java#L117-L147
As of 4.1, we don't enforce a unique value on the category.categoryvelocityvar_name field directly via SQL (constraint on this column) and you are currently enabled to create more than one category and get the same Velocity Var Name created for them.
Expected Behavior
Categories should have unique Velocity Var Names.
Possible Solution
Implement a logic for generating Velocity Var Names for Categories in the same way it's made for Fields and Content Types.
Known Workarounds
None
Steps to Reproduce (for bugs)
-
on Categories portlet, create the following categories.
- Category Name: Kids. Category Key: 3.
- Category Name: Kids. Category Key: 13

-
Delete Kids Category (the one with
3as Key). -
Re-add the Kids category with
3as Key. -
Both categories have the same Velocity Var Name.

Context
As per this ticket https://my.dotcms.com/tickets/detail.dot?id=3fcfead2-5893-42d7-9948-c958bfbf04d0, there was a problem on 3.7.1 related to categories, which is similar to this one https://github.com/dotCMS/core/issues/11088
These were the steps provided by customer.
- Create a category with name "kids" and key "3"
- The system auto-assigns the variable/id "kids"
- Create another category with name "kids" and key "13"
- The system auto-assigns the variable/id "kids1"
- Delete the category with name "kids" and key "3"
- Attempt to add new category with name "kids" and key "3." The pop out window shows the "Variable/ID" field is set to "kids" [screenshot attached]
- Press save, nothing happens in the front end
- In logs, we see the following "duplicate key" error:
[14/06/17 10:06:34:004 CDT] WARN util.JDBCExceptionReporter: SQL Error: 0, SQLState: 23505
[14/06/17 10:06:34:004 CDT] ERROR util.JDBCExceptionReporter: ERROR: duplicate key value violates unique constraint "category_category_velocity_var_name_key"
Detail: Key (category_velocity_var_name)=(kids1) already exists.
[14/06/17 10:06:34:005 CDT] ERROR impl.SessionImpl: Could not synchronize database state with session
[14/06/17 10:06:34:057 CDT] WARN util.JDBCExceptionReporter: SQL Error: 0, SQLState: 23505
[14/06/17 10:06:34:057 CDT] ERROR util.JDBCExceptionReporter: ERROR: duplicate key value violates unique constraint "pk_inode"
Detail: Key (inode)=(b658bea3-2a4a-4730-9774-8ed18e5209ba) already exists.
Although this specific behavior cannot be reproduced on 4.1, it happens on 3.7.1. Issue reported on this Ticket surfaced during these tests, so it needs some work.
Your Environment
- dotCMS 4.1.
- Any OS/Browser.
- Tomcat 8.0.18.
- Java 8u121.
- Postgres DB.
PR: https://github.com/dotCMS/core/pull/11900
When adding the integration test I found a small bug with the camel case of the suggested variable name, I implemented the fix to match camel case behavior before commits in this issue.
PR: https://github.com/dotCMS/core/pull/11958
Passed QA. Followed steps to reproduce. Currently categories are not being duplicated. Tested also with alphanumeric numbers. No categories were duplicated. Tested locally using the following environments: Upgrade from dotcms_3.6 with client dataset (db and assets) to master branch / commit a4f698e5ed4305f2f370d68cfaa72d09e5368bdd Ubuntu 14.04.1 / Postgres 9.3.7 / Chrome v54.0.2840.98 / Tomcat 8 / Java 8