saved searches: fix normalization of labels.

Fix saved searches to remove additional invalid characters from labels:

* Remove repeated spaces or underscores.
* Remove leading and trailing spaces or underscores.
* Normalize Unicode characters to NFC form.

Also add a fix script to renormalize labels in old saved searches. A few
problems with existing searches:

* Some saved searches somehow had labels containing NULL elements.
* Some had leading or trailing underscores.
* Some had repeated underscores.
* Some had non-English characters in uppercase.
This commit is contained in:
evazion
2021-01-09 21:18:43 -06:00
parent 7fbac34962
commit e788d8d0b6
3 changed files with 47 additions and 20 deletions

View File

@@ -110,6 +110,25 @@ class SavedSearchTest < ActiveSupport::TestCase
end
end
context "The #normalize_labels method" do
subject { build(:saved_search) }
should normalize_attribute(:labels).from(["FOO"]).to(["foo"])
should normalize_attribute(:labels).from([" foo"]).to(["foo"])
should normalize_attribute(:labels).from(["foo "]).to(["foo"])
should normalize_attribute(:labels).from(["___foo"]).to(["foo"])
should normalize_attribute(:labels).from(["foo___"]).to(["foo"])
should normalize_attribute(:labels).from(["foo\n"]).to(["foo"])
should normalize_attribute(:labels).from(["foo bar"]).to(["foo_bar"])
should normalize_attribute(:labels).from(["foo bar"]).to(["foo_bar"])
should normalize_attribute(:labels).from(["foo___bar"]).to(["foo_bar"])
should normalize_attribute(:labels).from([" _Foo Bar_ "]).to(["foo_bar"])
should normalize_attribute(:labels).from(["Я"]).to(["я"])
should normalize_attribute(:labels).from(["foo 1", "bar 2"]).to(["foo_1", "bar_2"])
should normalize_attribute(:labels).from(["foo", nil, "", " ", "bar"]).to(["foo", "bar"])
should normalize_attribute(:labels).from([nil, "", " "]).to([])
end
context "Populating a saved search" do
setup do
@saved_search = create(:saved_search, query: "bkub", user: @user)