<commit_before>#
# Copyright (C) 2012 - present Instructure, Inc.
#
# This file is part of Canvas.
#
# Canvas is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the Free
# Software Foundation, version 3 of the License.
#
# Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
# details.
#
# You should have received a copy of the GNU Affero General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.

require_relative 'helpers/wiki_and_tiny_common'

describe "Wiki pages and Tiny WYSIWYG editor features" do
  include_context "in-process server selenium tests"
  include WikiAndTinyCommon

  equation_button_selector = "div[aria-label='Insert Math Equation'] button"

  context "WYSIWYG generic as a teacher" do

    before(:each) do
      course_with_teacher_logged_in
    end

    def wysiwyg_state_setup(text = "1\n2\n3", val: false, html: false)
      get "/courses/#{@course.id}/pages/front-page/edit"
      wait_for_tiny(f("form.edit-form .edit-content"))

      if val == true
        add_text_to_tiny(text)
        validate_link(text)
      else
        if html
          add_html_to_tiny(text)
        else
          add_text_to_tiny_no_val(text)
        end
        select_all_wiki
      end
    end

    it "should type a web address link, save it, "\
    "and validate auto link plugin worked correctly", priority: "1", test_id: 312410 do
      text = "http://www.google.com/"
      wysiwyg_state_setup(text, val: true)
      save_wiki
      validate_link(text)
    end

    it "should remove web address link previously embedded, save it and persist", priority: "1", test_id: 312637 do
      text = "http://www.google.com/"
      wysiwyg_state_setup(text, val: true)

      select_all_wiki
      f('.mce-i-unlink').click
      save_wiki

      in_frame wiki_page_body_ifr_id do
        expect(f("#tinymce")).not_to contain_css('a')
      end
    end

    it "should switch views and handle html code" do
      wysiwyg_state_setup

      in_frame wiki_page_body_ifr_id do
        expect(ff("#tinymce p").length).to eq 3
      end
    end

    it "should add bullet lists", priority: "1", test_id: 307623 do
      wysiwyg_state_setup

      f(".mce-i-bullist").click
      in_frame wiki_page_body_ifr_id do
        expect(ff('#tinymce li').length).to eq 3
      end
    end

    it "should remove bullet lists", priority: "1", test_id: 535894 do
      text = "<ul><li>1</li><li>2</li><li>3</li></ul>"
      wysiwyg_state_setup(text, html: true)

      # editor window needs focus in chrome to enable bullet list button
      f("form.edit-form .edit-content").click if driver.browser == :chrome
      f(".mce-i-bullist").click

      in_frame wiki_page_body_ifr_id do
        expect(f("#tinymce")).not_to contain_css('li')
      end
    end

    it "should add numbered lists", priority: "1", test_id: 307625 do
      wysiwyg_state_setup

      f('.mce-i-numlist').click
      in_frame wiki_page_body_ifr_id do
        expect(ff('#tinymce li').length).to eq 3
      end
    end

    it "should remove numbered lists", priority: "1", test_id: 537619 do
      text = "<ol><li>1</li><li>2</li><li>3</li></ol>"
      wysiwyg_state_setup(text, html: true)

      # editor window needs focus in chrome to enable number list button
      f("form.edit-form .edit-content").click if driver.browser == :chrome
      f('.mce-i-numlist').click

      in_frame wiki_page_body_ifr_id do
        expect(f("#tinymce")).not_to contain_css('li')
      end
    end

    it "should change font color for all selected text", priority: "1", test_id: 285357 do
      wysiwyg_state_setup

      # order-dependent ID of the forecolor button
      f("#mceu_3 .mce-caret").click
      f(".mce-colorbutton-grid div[title='Red']").click
      validate_wiki_style_attrib("color", "rgb(255, 0, 0)", "p span")
    end

    it "should remove font color for all selected text", priority: "1", test_id: 469876 do
      text = "<p><span style=\"color: rgb(255, 0, 0);\">1</span></p>"
      wysiwyg_state_setup(text, html: true)

      # order-dependent ID of the forecolor button
      f("#mceu_3 .mce-caret").click
      f(".mce-colorbutton-grid div[title='No color']").click
      in_frame wiki_page_body_ifr_id do
        expect(f("#tinymce")).not_to contain_css('span')
      end
    end

    it "should change background font color", priority: "1", test_id: 298747 do
      wysiwyg_state_setup

      # order-dependent ID of the backcolor button
      f("#mceu_4 .mce-caret").click
      f(".mce-colorbutton-grid div[title='Red']").click
      validate_wiki_style_attrib("background-color", "rgb(255, 0, 0)", "p span")
    end

    it "should remove background font color", priority: "1", test_id: 474035 do
      text = "<p><span style=\"background-color: rgb(255, 0, 0);\">1</span></p>"
      wysiwyg_state_setup(text, html: true)

      # order-dependent ID of the backcolor button
      f("#mceu_4 .mce-caret").click
      f(".mce-colorbutton-grid div[title='No color']").click
      in_frame wiki_page_body_ifr_id do
        expect(f("#tinymce")).not_to contain_css('span')
      end
    end

    it "should change font size", priority: "1", test_id: 401375 do
      wysiwyg_state_setup

      # I'm so, so sorry...
      driver.find_element(:xpath, "//button/span[text()[contains(.,'12pt')]]").click
      driver.find_element(:xpath, "//span[text()[contains(.,'36pt')]]").click
      validate_wiki_style_attrib("font-size", "36pt", "p span")
    end

    it "should change and remove all custom formatting on selected text", priority: "1", test_id: 298748 do
      wysiwyg_state_setup
      driver.find_element(:xpath, "//button/span[text()[contains(.,'12pt')]]").click
      driver.find_element(:xpath, "//span[text()[contains(.,'36pt')]]").click
      validate_wiki_style_attrib("font-size", "36pt", "p span")
      f(".mce-i-removeformat").click
      validate_wiki_style_attrib_empty("p")
    end

    it 'should insert image using embed image widget', priority: "2", test_id: 397971 do
      wiki_page_tools_file_tree_setup
      fj('.mce-ico.mce-i-image').click
      wait_for_ajaximations
      widget = fj('.ui-dialog.ui-widget.ui-widget-content.ui-corner-all.ui-draggable.ui-dialog-buttons')
      widget.find_element(:link_text, 'Canvas').click
      wait_for_ajaximations
      widget.find_element(:link_text, 'Course files').click
      wait_for_ajaximations
      widget.find_element(:link_text, 'email.png').click
      fj('.btn-primary.ui-button.ui-widget.ui-state-default.ui-corner-all.ui-button-text-only').click
      wait_for_ajaximations
      fj('.btn.btn-primary.submit').click
      wait_for_ajaximations
      main = fj('#main')
      expect(main.find_element(:tag_name, 'img')).to have_attribute('height', '16')
      expect(main.find_element(:tag_name, 'img')).to have_attribute('width', '16')
      expect(main.find_element(:tag_name, 'img')).to have_attribute('alt', 'email.png')
    end

    it "should indent and remove indentation for embedded images" do
      wiki_page_tools_file_tree_setup

      f('#editor_tabs .ui-tabs-nav li:nth-child(3) a').click
      wait_for_ajaximations

      @image_list.find_element(:css, '.img_link').click

      select_all_wiki
      f('.mce-i-indent').click
      validate_wiki_style_attrib("padding-left", "30px", "p")
      f('.mce-i-outdent').click
      validate_wiki_style_attrib_empty("p")
    end<commit_msg>Fix image insert flaky spec<commit_after>#
# Copyright (C) 2012 - present Instructure, Inc.
#
# This file is part of Canvas.
#
# Canvas is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the Free
# Software Foundation, version 3 of the License.
#
# Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
# details.
#
# You should have received a copy of the GNU Affero General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.

require_relative 'helpers/wiki_and_tiny_common'

describe "Wiki pages and Tiny WYSIWYG editor features" do
  include_context "in-process server selenium tests"
  include WikiAndTinyCommon

  equation_button_selector = "div[aria-label='Insert Math Equation'] button"

  context "WYSIWYG generic as a teacher" do

    before(:each) do
      course_with_teacher_logged_in
    end

    def wysiwyg_state_setup(text = "1\n2\n3", val: false, html: false)
      get "/courses/#{@course.id}/pages/front-page/edit"
      wait_for_tiny(f("form.edit-form .edit-content"))

      if val == true
        add_text_to_tiny(text)
        validate_link(text)
      else
        if html
          add_html_to_tiny(text)
        else
          add_text_to_tiny_no_val(text)
        end
        select_all_wiki
      end
    end

    it "should type a web address link, save it, "\
    "and validate auto link plugin worked correctly", priority: "1", test_id: 312410 do
      text = "http://www.google.com/"
      wysiwyg_state_setup(text, val: true)
      save_wiki
      validate_link(text)
    end

    it "should remove web address link previously embedded, save it and persist", priority: "1", test_id: 312637 do
      text = "http://www.google.com/"
      wysiwyg_state_setup(text, val: true)

      select_all_wiki
      f('.mce-i-unlink').click
      save_wiki

      in_frame wiki_page_body_ifr_id do
        expect(f("#tinymce")).not_to contain_css('a')
      end
    end

    it "should switch views and handle html code" do
      wysiwyg_state_setup

      in_frame wiki_page_body_ifr_id do
        expect(ff("#tinymce p").length).to eq 3
      end
    end

    it "should add bullet lists", priority: "1", test_id: 307623 do
      wysiwyg_state_setup

      f(".mce-i-bullist").click
      in_frame wiki_page_body_ifr_id do
        expect(ff('#tinymce li').length).to eq 3
      end
    end

    it "should remove bullet lists", priority: "1", test_id: 535894 do
      text = "<ul><li>1</li><li>2</li><li>3</li></ul>"
      wysiwyg_state_setup(text, html: true)

      # editor window needs focus in chrome to enable bullet list button
      f("form.edit-form .edit-content").click if driver.browser == :chrome
      f(".mce-i-bullist").click

      in_frame wiki_page_body_ifr_id do
        expect(f("#tinymce")).not_to contain_css('li')
      end
    end

    it "should add numbered lists", priority: "1", test_id: 307625 do
      wysiwyg_state_setup

      f('.mce-i-numlist').click
      in_frame wiki_page_body_ifr_id do
        expect(ff('#tinymce li').length).to eq 3
      end
    end

    it "should remove numbered lists", priority: "1", test_id: 537619 do
      text = "<ol><li>1</li><li>2</li><li>3</li></ol>"
      wysiwyg_state_setup(text, html: true)

      # editor window needs focus in chrome to enable number list button
      f("form.edit-form .edit-content").click if driver.browser == :chrome
      f('.mce-i-numlist').click

      in_frame wiki_page_body_ifr_id do
        expect(f("#tinymce")).not_to contain_css('li')
      end
    end

    it "should change font color for all selected text", priority: "1", test_id: 285357 do
      wysiwyg_state_setup

      # order-dependent ID of the forecolor button
      f("#mceu_3 .mce-caret").click
      f(".mce-colorbutton-grid div[title='Red']").click
      validate_wiki_style_attrib("color", "rgb(255, 0, 0)", "p span")
    end

    it "should remove font color for all selected text", priority: "1", test_id: 469876 do
      text = "<p><span style=\"color: rgb(255, 0, 0);\">1</span></p>"
      wysiwyg_state_setup(text, html: true)

      # order-dependent ID of the forecolor button
      f("#mceu_3 .mce-caret").click
      f(".mce-colorbutton-grid div[title='No color']").click
      in_frame wiki_page_body_ifr_id do
        expect(f("#tinymce")).not_to contain_css('span')
      end
    end

    it "should change background font color", priority: "1", test_id: 298747 do
      wysiwyg_state_setup

      # order-dependent ID of the backcolor button
      f("#mceu_4 .mce-caret").click
      f(".mce-colorbutton-grid div[title='Red']").click
      validate_wiki_style_attrib("background-color", "rgb(255, 0, 0)", "p span")
    end

    it "should remove background font color", priority: "1", test_id: 474035 do
      text = "<p><span style=\"background-color: rgb(255, 0, 0);\">1</span></p>"
      wysiwyg_state_setup(text, html: true)

      # order-dependent ID of the backcolor button
      f("#mceu_4 .mce-caret").click
      f(".mce-colorbutton-grid div[title='No color']").click
      in_frame wiki_page_body_ifr_id do
        expect(f("#tinymce")).not_to contain_css('span')
      end
    end

    it "should change font size", priority: "1", test_id: 401375 do
      wysiwyg_state_setup

      # I'm so, so sorry...
      driver.find_element(:xpath, "//button/span[text()[contains(.,'12pt')]]").click
      driver.find_element(:xpath, "//span[text()[contains(.,'36pt')]]").click
      validate_wiki_style_attrib("font-size", "36pt", "p span")
    end

    it "should change and remove all custom formatting on selected text", priority: "1", test_id: 298748 do
      wysiwyg_state_setup
      driver.find_element(:xpath, "//button/span[text()[contains(.,'12pt')]]").click
      driver.find_element(:xpath, "//span[text()[contains(.,'36pt')]]").click
      validate_wiki_style_attrib("font-size", "36pt", "p span")
      f(".mce-i-removeformat").click
      validate_wiki_style_attrib_empty("p")
    end

    it 'should insert image using embed image widget', priority: "2", test_id: 397971 do
      wiki_page_tools_file_tree_setup
      f('.mce-ico.mce-i-image').click
      wait_for_ajaximations
      widget = f('.ui-dialog.ui-widget.ui-widget-content.ui-corner-all.ui-draggable.ui-dialog-buttons')
      widget.find_element(:link_text, 'Canvas').click
      wait_for_ajaximations
      widget.find_element(:link_text, 'Course files').click
      wait_for_ajaximations
      widget.find_element(:link_text, 'email.png').click
      f('.btn-primary.ui-button.ui-widget.ui-state-default.ui-corner-all.ui-button-text-only').click
      wait_for_ajaximations
      f('.btn.btn-primary.submit').click
      wait_for_new_page_load
      main = f('#main')
      expect(main.find_element(:tag_name, 'img')).to have_attribute('height', '16')
      expect(main.find_element(:tag_name, 'img')).to have_attribute('width', '16')
      expect(main.find_element(:tag_name, 'img')).to have_attribute('alt', 'email.png')
    end

    it "should indent and remove indentation for embedded images" do
      wiki_page_tools_file_tree_setup

      f('#editor_tabs .ui-tabs-nav li:nth-child(3) a').click
      wait_for_ajaximations

      @image_list.find_element(:css, '.img_link').click

      select_all_wiki
      f('.mce-i-indent').click
      validate_wiki_style_attrib("padding-left", "30px", "p")
      f('.mce-i-outdent').click
      validate_wiki_style_attrib_empty("p")
    end
{'fraction_non_alphanumeric': 0.12614140360031306, 'fraction_numerical': 0.01995825723975998, 'mean_word_length': 3.156410951477365, 'pattern_counts': {'":': 0, '<': 53, '<?xml version=': 0, '>': 53, 'https://': 0, 'lorem ipsum': 0, 'www.': 6, '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': '6737977', 'n_tokens_mistral': 5325, 'n_tokens_neox': 5016, 'n_words': 1261}