<commit_before>require 'spec_helper'

describe 'cms_agents_addons_file', type: :feature, dbscope: :example, js: true do
  let(:site){ cms_site }
  let!(:form) { create(:cms_form, cur_site: site, state: 'public', sub_type: 'entry', group_ids: cms_user.group_ids) }
  let!(:column1) do
    create(:cms_column_file_upload, cur_site: site, cur_form: form, required: "optional", file_type: "video", order: 1)
  end
  let!(:node) { create :article_node_page, cur_site: site, st_form_ids: [ form.id ], st_form_default_id: form.id }
  let(:filename) { "#{unique_id}.png" }

  before do
    login_cms_user
  end

  shared_examples "file dialog is" do
    context "click" do
      it do
        within "#ajax-box" do
          expect(page).to have_css('.file-view', text: filename)
          wait_cbox_close do
            wait_event_to_fire "ss:ajaxFileSelected", "#addon-cms-agents-addons-form-page .ajax-box" do
              click_on filename
            end
          end
        end

        within "#addon-cms-agents-addons-form-page .column-value-cms-column-fileupload" do
          within '.file-view' do
            expect(page).to have_css('.name', text: filename)
          end
        end
      end
    end

    context "edit" do
      it do
        within "#ajax-box" do
          within ".file-view[data-file-id='#{file.id}']" do
            expect(page).to have_css(".name", text: filename)
            click_on I18n.t("ss.buttons.edit")
          end
        end

        within "#ajax-box" do
          expect(page).to have_css(".ss-image-edit-canvas")
        end
      end
    end

    context "delete" do
      it do
        within "#ajax-box" do
          within ".file-view[data-file-id='#{file.id}']" do
            expect(page).to have_css(".name", text: filename)
            wait_event_to_fire "ss:ajaxFileRemoved", "#addon-cms-agents-addons-form-page .ajax-box" do
              page.accept_confirm do
                click_on I18n.t("ss.buttons.delete")
              end
            end
          end
        end

        expect { file.reload }.to raise_error Mongoid::Errors::DocumentNotFound
      end
    end

    context "save and click" do
      it do
        within "#ajax-box" do
          attach_file "item[in_files][]", "#{Rails.root}/spec/fixtures/ss/file/keyvisual.jpg"
          click_button I18n.t("ss.buttons.save")
          expect(page).to have_css('.file-view', text: 'keyvisual.jpg')
          wait_cbox_close do
            wait_event_to_fire "ss:ajaxFileSelected", "#addon-cms-agents-addons-form-page .ajax-box" do
              click_on 'keyvisual.jpg'
            end
          end
        end

        within "#addon-cms-agents-addons-form-page .column-value-cms-column-fileupload" do
          within '.file-view' do
            expect(page).to have_css('.name', text: 'keyvisual.jpg')
          end
        end
      end
    end

    context "attach" do
      it do
        within "#ajax-box" do
          attach_file "item[in_files][]", "#{Rails.root}/spec/fixtures/ss/file/keyvisual.jpg"
          wait_cbox_close do
            wait_event_to_fire "ss:ajaxFileSelected", "#addon-cms-agents-addons-form-page .ajax-box" do
              click_button I18n.t("ss.buttons.attach")
            end
          end
        end

        within "#addon-cms-agents-addons-form-page .column-value-cms-column-fileupload" do
          within '.file-view' do
            expect(page).to have_css('.name', text: 'keyvisual.jpg')
          end
        end
      end
    end
  end

  shared_examples "several operations on file dialog" do
    before do
      visit article_pages_path(site: site, cid: node)
      click_on I18n.t("ss.links.new")

      within "#addon-cms-agents-addons-form-page" do
        within ".column-value-palette" do
          wait_event_to_fire("ss:columnAdded") do
            click_on column1.name
          end
        end
        within ".column-value-cms-column-fileupload" do
          wait_cbox_open do
            click_on button_label
          end
        end
      end

      within "#ajax-box" do
        page.execute_script("SS_AjaxFile.firesEvents = true;")
      end
    end

    context "default" do
      it_behaves_like "file dialog is"
    end

    context "after file is saved" do
      before do
        within "#ajax-box" do
          attach_file "item[in_files][]", "#{Rails.root}/spec/fixtures/ss/logo.png"
          click_button I18n.t("ss.buttons.save")
          expect(page).to have_css('.file-view', text: 'logo.png')
        end
      end

      it_behaves_like "file dialog is"
    end

    context "after edit dialog is canceled" do
      before do
        within "#ajax-box" do
          within ".file-view[data-file-id='#{file.id}']" do
            expect(page).to have_css(".name", text: filename)
            click_on I18n.t("ss.buttons.edit")
          end
        end

        within "#ajax-box" do
          expect(page).to have_css(".ss-image-edit-canvas")
          within "#ajax-form" do
            click_on I18n.t("ss.buttons.cancel")
          end
        end
      end

      it_behaves_like "file dialog is"
    end

    context "after edit dialog is saved" do
      before do
        within "#ajax-box" do
          within ".file-view[data-file-id='#{file.id}']" do
            expect(page).to have_css(".name", text: filename)
            click_on I18n.t("ss.buttons.edit")
          end
        end

        within "#ajax-box" do
          expect(page).to have_css(".ss-image-edit-canvas")
          within "#ajax-form" do
            click_on I18n.t("ss.buttons.save")
          end
        end
      end

      it_behaves_like "file dialog is"
    end
  end

  context "with ss/temp_file" do
    let!(:file) do
      tmp_ss_file(user: cms_user, site: site, basename: filename, contents: "#{Rails.root}/spec/fixtures/ss/logo.png")
    end
    let(:button_label) { I18n.t("ss.buttons.upload") }

    it_behaves_like "several operations on file dialog"
  end

  context "with ss/user_file" do
    let!(:file) do
      tmp_ss_file(
        SS::UserFile, model: "ss/user_file", user: cms_user, basename: filename,
        contents: "#{Rails.root}/spec/fixtures/ss/logo.png"
      )
    end
    let(:button_label) { I18n.t("sns.user_file") }

    it_behaves_like "several operations on file dialog"
  end

  context "with cms/file" do
    let!(:file) do<commit_msg>[fixup] use ".prep" for reloading contents in cbox and better file selection handling (#3907)<commit_after>require 'spec_helper'

describe 'cms_agents_addons_file', type: :feature, dbscope: :example, js: true do
  let(:site){ cms_site }
  let!(:form) { create(:cms_form, cur_site: site, state: 'public', sub_type: 'entry', group_ids: cms_user.group_ids) }
  let!(:column1) do
    create(:cms_column_file_upload, cur_site: site, cur_form: form, required: "optional", file_type: "video", order: 1)
  end
  let!(:node) { create :article_node_page, cur_site: site, st_form_ids: [ form.id ], st_form_default_id: form.id }
  let(:filename) { "#{unique_id}.png" }

  before do
    login_cms_user
  end

  shared_examples "file dialog is" do
    context "click" do
      it do
        within "#ajax-box" do
          expect(page).to have_css('.file-view', text: filename)
          wait_cbox_close do
            wait_event_to_fire "ss:ajaxFileSelected", "#addon-cms-agents-addons-form-page .ajax-box" do
              click_on filename
            end
          end
        end

        within "#addon-cms-agents-addons-form-page .column-value-cms-column-fileupload" do
          within '.file-view' do
            expect(page).to have_css('.name', text: filename)
          end
        end
      end
    end

    context "edit" do
      it do
        within "#ajax-box" do
          within ".file-view[data-file-id='#{file.id}']" do
            expect(page).to have_css(".name", text: filename)
            click_on I18n.t("ss.buttons.edit")
          end
        end

        within "#ajax-box" do
          expect(page).to have_css(".ss-image-edit-canvas")
        end
      end
    end

    context "delete" do
      it do
        within "#ajax-box" do
          within ".file-view[data-file-id='#{file.id}']" do
            expect(page).to have_css(".name", text: filename)
            wait_event_to_fire "ss:ajaxFileRemoved", "#addon-cms-agents-addons-form-page .ajax-box" do
              page.accept_confirm do
                click_on I18n.t("ss.buttons.delete")
              end
            end
          end
        end

        expect { file.reload }.to raise_error Mongoid::Errors::DocumentNotFound
      end
    end

    context "save and click" do
      it do
        within "#ajax-box" do
          attach_file "item[in_files][]", "#{Rails.root}/spec/fixtures/ss/file/keyvisual.jpg"
          click_button I18n.t("ss.buttons.save")
          expect(page).to have_css('.file-view', text: 'keyvisual.jpg')
          wait_cbox_close do
            wait_event_to_fire "ss:ajaxFileSelected", "#addon-cms-agents-addons-form-page .ajax-box" do
              click_on 'keyvisual.jpg'
            end
          end
        end

        within "#addon-cms-agents-addons-form-page .column-value-cms-column-fileupload" do
          within '.file-view' do
            expect(page).to have_css('.name', text: 'keyvisual.jpg')
          end
        end
      end
    end

    context "attach" do
      it do
        within "#ajax-box" do
          attach_file "item[in_files][]", "#{Rails.root}/spec/fixtures/ss/file/keyvisual.jpg"
          wait_cbox_close do
            wait_event_to_fire "ss:ajaxFileSelected", "#addon-cms-agents-addons-form-page .ajax-box" do
              click_button I18n.t("ss.buttons.attach")
            end
          end
        end

        within "#addon-cms-agents-addons-form-page .column-value-cms-column-fileupload" do
          within '.file-view' do
            expect(page).to have_css('.name', text: 'keyvisual.jpg')
          end
        end
      end
    end
  end

  shared_examples "several operations on file dialog" do
    before do
      visit article_pages_path(site: site, cid: node)
      click_on I18n.t("ss.links.new")

      within "#addon-cms-agents-addons-form-page" do
        within ".column-value-palette" do
          wait_event_to_fire("ss:columnAdded") do
            click_on column1.name
          end
        end
        within ".column-value-cms-column-fileupload" do
          wait_cbox_open do
            click_on button_label
          end
        end
      end

      within "#ajax-box" do
        page.execute_script("SS_AjaxFile.firesEvents = true;")
      end
    end

    context "default" do
      it_behaves_like "file dialog is"
    end

    context "after file is saved" do
      before do
        within "#ajax-box" do
          attach_file "item[in_files][]", "#{Rails.root}/spec/fixtures/ss/logo.png"
          click_button I18n.t("ss.buttons.save")
          expect(page).to have_css('.file-view', text: 'logo.png')
        end
      end

      it_behaves_like "file dialog is"
    end

    context "after edit dialog is canceled" do
      before do
        within "#ajax-box" do
          within ".file-view[data-file-id='#{file.id}']" do
            expect(page).to have_css(".name", text: filename)
            click_on I18n.t("ss.buttons.edit")
          end
        end

        within "#ajax-box" do
          expect(page).to have_css(".ss-image-edit-canvas")
          within "#ajax-form" do
            click_on I18n.t("ss.buttons.cancel")
          end
        end
      end

      it_behaves_like "file dialog is"
    end

    context "after edit dialog is saved" do
      before do
        within "#ajax-box" do
          within ".file-view[data-file-id='#{file.id}']" do
            expect(page).to have_css(".name", text: filename)
            click_on I18n.t("ss.buttons.edit")
          end
        end

        within "#ajax-box" do
          expect(page).to have_css(".ss-image-edit-canvas")
          within "#ajax-form" do
            click_on I18n.t("ss.buttons.save")
          end
        end
      end

      it_behaves_like "file dialog is"
    end
  end

  context "with ss/temp_file" do
    let!(:file) do
      tmp_ss_file(
        Cms::TempFile, user: cms_user, site: site, node: node, basename: filename,
        contents: "#{Rails.root}/spec/fixtures/ss/logo.png"
      )
    end
    let(:button_label) { I18n.t("ss.buttons.upload") }

    it_behaves_like "several operations on file dialog"
  end

  context "with ss/user_file" do
    let!(:file) do
      tmp_ss_file(
        SS::UserFile, model: "ss/user_file", user: cms_user, basename: filename,
        contents: "#{Rails.root}/spec/fixtures/ss/logo.png"
      )
    end
    let(:button_label) { I18n.t("sns.user_file") }

    it_behaves_like "several operations on file dialog"
  end

  context "with cms/file" do
    let!(:file) do
{'fraction_non_alphanumeric': 0.11968271249708376, 'fraction_numerical': 0.00451045960027996, 'mean_word_length': 2.0301602262016964, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '528061', 'n_tokens_mistral': 4277, 'n_tokens_neox': 4108, 'n_words': 940}