<commit_before>module Releaf
  # Releaf::TestHelpers provides a facility to simplify admin functionality testing
  module TestHelpers
    def postgresql?
      adapter_name == 'PostgreSQL'
    end

    def mysql?
      adapter_name == "Mysql2"
    end

    def adapter_name
      ActiveRecord::Base.connection.adapter_name
    end

    def auth_as_user(full_login = false, factory = :user)
      if factory.is_a? Releaf::Permissions::User
        user = factory
      else
        user = create(factory)
      end
      if full_login
        visit "/"
        within("form.login") do
          fill_in 'Email', with: user.email
          fill_in 'Password', with: <PASSWORD>
        end

        click_button 'Sign in'
      else
        login_as user
      end

      return user
    end

    def stub_settings(values)
      unless @releaf_settings_default_stubbed
        allow(Releaf::Settings).to receive(:[]).and_call_original
        @releaf_settings_default_stubbed = true
      end

      values.each_pair do|key, value|
        allow(Releaf::Settings).to receive(:[]).with(key).and_return(value)
      end
    end

    def update_resource(&block)
      within "form.edit-resource" do
        yield
      end
      save_and_check_response "Update succeeded"
    end

    def create_resource(&block)
      click_link "Create new resource" unless first("form.new-resource")
      within "form.new-resource" do
        yield
      end
      save_and_check_response "Create succeeded"
    end

    def within_search(&block)
      within("form.search") do
        yield
      end
    end

    def search(text)
      within_search do
        fill_in 'search', with: text
      end
    end

    def within_dialog(&block)
      within(".dialog.initialized") do
        yield
      end
    end

    def close_dialog
      within_dialog do
        find("a[data-type='cancel']").click
      end
      expect(page).to have_no_css(".dialog")
    end

    def wait_for_all_richtexts
      # wait for all ckeditors to fully initialize before moving on.
      # otherwise the page sometimes produces random js errors in fast tests
      number_of_richtexts = page.all('.field.type-richtext').length
      if (number_of_richtexts > 0)
        expect(page).to have_css(".ckeditor-initialized", visible: false, count: number_of_richtexts)
      end
    end

    def save_and_check_response(status_text)
      wait_for_all_richtexts
      click_button 'Save'
      expect(page).to have_css('body > .notifications .notification[data-id="resource_status"][data-type="success"]', text: status_text)
      wait_for_all_richtexts
    end
<commit_msg>Support localized richtext fields in wait_for_all_richtexts test helper<commit_after>module Releaf
  # Releaf::TestHelpers provides a facility to simplify admin functionality testing
  module TestHelpers
    def postgresql?
      adapter_name == 'PostgreSQL'
    end

    def mysql?
      adapter_name == "Mysql2"
    end

    def adapter_name
      ActiveRecord::Base.connection.adapter_name
    end

    def auth_as_user(full_login = false, factory = :user)
      if factory.is_a? Releaf::Permissions::User
        user = factory
      else
        user = create(factory)
      end
      if full_login
        visit "/"
        within("form.login") do
          fill_in 'Email', with: user.email
          fill_in 'Password', with: <PASSWORD>
        end

        click_button 'Sign in'
      else
        login_as user
      end

      return user
    end

    def stub_settings(values)
      unless @releaf_settings_default_stubbed
        allow(Releaf::Settings).to receive(:[]).and_call_original
        @releaf_settings_default_stubbed = true
      end

      values.each_pair do|key, value|
        allow(Releaf::Settings).to receive(:[]).with(key).and_return(value)
      end
    end

    def update_resource(&block)
      within "form.edit-resource" do
        yield
      end
      save_and_check_response "Update succeeded"
    end

    def create_resource(&block)
      click_link "Create new resource" unless first("form.new-resource")
      within "form.new-resource" do
        yield
      end
      save_and_check_response "Create succeeded"
    end

    def within_search(&block)
      within("form.search") do
        yield
      end
    end

    def search(text)
      within_search do
        fill_in 'search', with: text
      end
    end

    def within_dialog(&block)
      within(".dialog.initialized") do
        yield
      end
    end

    def close_dialog
      within_dialog do
        find("a[data-type='cancel']").click
      end
      expect(page).to have_no_css(".dialog")
    end

    def wait_for_all_richtexts
      # wait for all ckeditors to fully initialize before moving on.
      # otherwise the page sometimes produces random js errors in fast tests
      number_of_normal_richtexts = page.all('.field.type-richtext:not(.i18n)').length
      number_of_localized_richtexts = page.all('.field.type-richtext.i18n .localization', visible: false).length
      number_of_richtexts = number_of_normal_richtexts + number_of_localized_richtexts
      if (number_of_richtexts > 0)
        expect(page).to have_css(".ckeditor-initialized", visible: false, count: number_of_richtexts)
      end
    end


    def save_and_check_response(status_text)
      wait_for_all_richtexts
      click_button 'Save'
      expect(page).to have_css('body > .notifications .notification[data-id="resource_status"][data-type="success"]', text: status_text)
      wait_for_all_richtexts
    end

{'fraction_non_alphanumeric': 0.08226592098141801, 'fraction_numerical': 0.0014432617716038246, 'mean_word_length': 2.5675675675675675, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2945213', 'n_tokens_mistral': 1664, 'n_tokens_neox': 1636, 'n_words': 397}