<commit_before>/**
 * Copyright (C) 2013 <NAME> (born 1979)
 *
 * This program 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, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program 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/>.
 */

package test.e2e.code

import com.debiki.core.Prelude._
import TestSiteCreator._



object TestSiteCreator {

  /** Sometimes I don't empty the database different specs, so it's best
    * to never reuse a website id — otherwise there'll be "website already created"
    * errors.
    */
  private var nextWebsiteId = 0

}



/** Creates test sites, via /-/create-site/...
  */
trait TestSiteCreator extends TestLoginner {
  self: DebikiBrowserSpec =>

  private var knownWindowHandles = Set[String]()

  private var currentSiteId = -1

  val DefaultHomepageTitle = "Default Homepage"


  def createSiteStartPage = new Page {
    val url = s"http://$newSiteDomain/-/create-site"
  }

  def createForumStartPageUrl = s"http://$newSiteDomain/-/create-forum"

  def createEmbeddedCommentsSiteStartPage = new Page {
    val url = s"http://$newSiteDomain/-/create-embedded-site"
  }

  def nextSiteName(): String = {
    nextWebsiteId += 1
    currentSiteId = nextWebsiteId
    s"test-site-$nextWebsiteId"
  }

  def currentSiteHomepageUrl = s"http://test-site-$currentSiteId.$newSiteDomain/"


  def clickCreateForum(login: () => Unit, siteName: String = null): String = {
    val name =
      if (siteName ne null) siteName
      else nextSiteName()

    info("create forum $name")

    go to createForumStartPageUrl
    login()

    click on "website-name"
    enter(name)
    click on "accepts-terms"
    click on cssSelector("input[type=submit]")

    name
  }


  def clickChooseSiteType(siteType: debiki.SiteCreator.NewSiteType) {
    click on "site-type"
    val siteTypeOptionId = siteType match {
      case debiki.SiteCreator.NewSiteType.SimpleSite => "new-simple-site"
      case debiki.SiteCreator.NewSiteType.Blog => "new-blog"
      case debiki.SiteCreator.NewSiteType.Forum => "new-forum"
      case debiki.SiteCreator.NewSiteType.EmbeddedComments =>
        // This is tested elsewere, namely in CreateEmbeddedCommentsSiteSpec.scala.
        assErr("DwE17wfh3", "Broken test")
    }
    click on siteTypeOptionId
    click on cssSelector("input[type=submit]")
    eventually {
      find("website-name") must be ('defined)
    }
  }


  def clickWelcomeLoginToDashboard(login: () => Unit, newSiteName: String) {
    viewNewSiteWelcomePageAndContinue()
    info("login to admin dashboard")
    login()
    verifyIsOnAdminPage(newSiteName)
  }


  def viewNewSiteWelcomePageAndContinue() {
    // We should now be on page /-/create-site/welcome-owner.
    // There should be only one link, which takes you to /-/admin/.
    info("view welcome owner page, click continue link")
    eventually {
      assert(pageSource contains "Website created")
    }
    click on cssSelector("a")
  }


  private def verifyIsOnAdminPage(newSiteName: String) {
    info("get to the admin page")
    eventually {
      assert(pageSource contains "Configure the Site")
    }
    // Currently, because I'm serving a static page and rendering the contents using
    // AngularDart, the page is located at something like:
    //   /-/assets/43/admin-dart-build/web/admin.html
    webDriver.getCurrentUrl() must include ("/-/assets/")
    webDriver.getCurrentUrl() must include ("admin-dart-build/web/admin.html")

    // When the page has a better address, revert to:
    //    ... must fullyMatch regex s"${originOf(newSiteName)}/-/admin/#/"
  }


  def clickGoToSiteFindHomepage(siteName: String) {
    go to originOf(siteName)
    Thread.sleep(1000 * 1000)
    ???
  }

  def clickGoToSiteFindBlog(siteName: String) {
    go to originOf(siteName)
    Thread.sleep(1000 * 1000)
    ???
  }

  def clickGoToSiteFindForum(siteName: String) {
    go to originOf(siteName)
    eventually {
      pageSource must include ("New Forum Title")
    }
  }


  private def goToHomepage() {
    // No idea why, but this finds the elem and then throws an error that """element
    // is not attached to the page document""": (and eventually {...} doesn't work)<commit_msg>Fix broken E2E test.<commit_after>/**
 * Copyright (C) 2013 <NAME> (born 1979)
 *
 * This program 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, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program 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/>.
 */

package test.e2e.code

import com.debiki.core.Prelude._
import TestSiteCreator._



object TestSiteCreator {

  /** Sometimes I don't empty the database different specs, so it's best
    * to never reuse a website id — otherwise there'll be "website already created"
    * errors.
    */
  private var nextWebsiteId = 0

}



/** Creates test sites, via /-/create-site/...
  */
trait TestSiteCreator extends TestLoginner {
  self: DebikiBrowserSpec =>

  private var knownWindowHandles = Set[String]()

  private var currentSiteId = -1

  val DefaultHomepageTitle = "Default Homepage"


  def createSiteStartPage = new Page {
    val url = s"http://$newSiteDomain/-/create-site"
  }

  def createForumStartPageUrl = s"http://$newSiteDomain/-/create-forum"

  def createEmbeddedCommentsSiteStartPage = new Page {
    val url = s"http://$newSiteDomain/-/create-embedded-site"
  }

  def nextSiteName(): String = {
    nextWebsiteId += 1
    currentSiteId = nextWebsiteId
    s"test-site-$nextWebsiteId"
  }

  def currentSiteHomepageUrl = s"http://test-site-$currentSiteId.$newSiteDomain/"


  def clickCreateForum(login: () => Unit, siteName: String = null): String = {
    val name =
      if (siteName ne null) siteName
      else nextSiteName()

    info("create forum $name")

    go to createForumStartPageUrl
    login()

    click on "website-name"
    enter(name)
    click on "accepts-terms"
    click on cssSelector("input[type=submit]")

    name
  }


  def clickChooseSiteType(siteType: debiki.SiteCreator.NewSiteType) {
    click on "site-type"
    val siteTypeOptionId = siteType match {
      case debiki.SiteCreator.NewSiteType.SimpleSite => "new-simple-site"
      case debiki.SiteCreator.NewSiteType.Blog => "new-blog"
      case debiki.SiteCreator.NewSiteType.Forum => "new-forum"
      case debiki.SiteCreator.NewSiteType.EmbeddedComments =>
        // This is tested elsewere, namely in CreateEmbeddedCommentsSiteSpec.scala.
        assErr("DwE17wfh3", "Broken test")
    }
    click on siteTypeOptionId
    click on cssSelector("input[type=submit]")
    eventually {
      find("website-name") must be ('defined)
    }
  }


  def clickWelcomeLoginToDashboard(login: () => Unit, newSiteName: String) {
    viewNewSiteWelcomePageAndContinue()
    info("login to admin dashboard")
    login()
    verifyIsOnAdminPage(newSiteName)
  }


  def viewNewSiteWelcomePageAndContinue() {
    // We should now be on page /-/create-site/welcome-owner.
    // There should be only one link, which takes you to /-/admin/.
    info("view welcome owner page, click continue link")
    eventually {
      assert(pageSource contains "Website created")
    }
    click on cssSelector("a")
  }


  private def verifyIsOnAdminPage(newSiteName: String) {
    info("get to the admin page")
    eventually {
      assert(pageSource contains "Return to site")
    }
    webDriver.getCurrentUrl() must startWith (s"${originOf(newSiteName)}/-/admin/")
  }


  def clickGoToSiteFindHomepage(siteName: String) {
    go to originOf(siteName)
    Thread.sleep(1000 * 1000)
    ???
  }

  def clickGoToSiteFindBlog(siteName: String) {
    go to originOf(siteName)
    Thread.sleep(1000 * 1000)
    ???
  }

  def clickGoToSiteFindForum(siteName: String) {
    go to originOf(siteName)
    eventually {
      pageSource must include ("New Forum Title")
    }
  }


  private def goToHomepage() {
    // No idea why, but this finds the elem and then throws an error that """element
    // is not attached to the page document""": (and eventually {...} doesn't work)
{'fraction_non_alphanumeric': 0.10231316725978648, 'fraction_numerical': 0.007451067615658363, 'mean_word_length': 3.260066319279962, 'pattern_counts': {'":': 2, '<': 7, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '3611775', 'n_tokens_mistral': 2847, 'n_tokens_neox': 2637, 'n_words': 834}