Fix url encoding of ampersand in corpusStats value

Change-Id: I17e72a979df5ed69f8de2fd762dd33fd121214c9
diff --git a/R/KorAPCorpusStats.R b/R/KorAPCorpusStats.R
index ab06382..655e9b9 100644
--- a/R/KorAPCorpusStats.R
+++ b/R/KorAPCorpusStats.R
@@ -26,6 +26,7 @@
 #' @param as.df return result as data frame instead of as S4 object?
 #' @return `KorAPCorpusStats` object with the slots `documents`, `tokens`, `sentences`, `paragraphs`
 #'
+#' @importFrom urltools url_encode
 #' @examples
 #'
 #' \dontrun{
@@ -50,7 +51,7 @@
              URLencode(enc2utf8(vc), reserved = TRUE))
     log_info(verbose, "Getting size of virtual corpus \"", vc, "\"", sep = "")
     res <- apiCall(kco, url)
-    webUIRequestUrl <- paste0(kco@KorAPUrl, sprintf("?q=<base/s=t>&cq=%s", URLencode(enc2utf8(vc))))
+    webUIRequestUrl <- paste0(kco@KorAPUrl, sprintf("?q=<base/s=t>&cq=%s", url_encode(enc2utf8(vc))))
     if(is.null(res)) {
       res <- data.frame(documents=NA, tokens=NA, sentences=NA, paragraphs=NA)
     }
diff --git a/tests/testthat/test-corpusStats.R b/tests/testthat/test-corpusStats.R
index b06a54f..345dd91 100644
--- a/tests/testthat/test-corpusStats.R
+++ b/tests/testthat/test-corpusStats.R
@@ -1,11 +1,20 @@
 test_that("corpusStats works", {
   skip_if_offline()
-  stats <- new("KorAPConnection") %>% corpusStats()
+  stats <- new("KorAPConnection") %>% corpusStats("pubDate since 2020 & pubDate until 2021")
   expect_gt(stats@tokens, 0)
   expect_gt(stats@paragraphs, 0)
   expect_gt(stats@documents, 0)
+  expect(grepl("%26", stats@webUIRequestUrl), "webUIRequestUrl not properly url encoded")
 })
 
+test_that("corpusStats with result as df works", {
+  skip_if_offline()
+  stats <- new("KorAPConnection") %>% corpusStats("pubDate since 2020 & pubDate until 2021", as.df = TRUE)
+  expect_gt(stats$tokens, 0)
+  expect_gt(stats$paragraphs, 0)
+  expect_gt(stats$documents, 0)
+  expect(grepl("%26", stats$webUIRequestUrl), "webUIRequestUrl not properly url encoded")
+})
 
 test_that("Printing corpusStats for the whole corpus works", {
   skip_if_offline()
@@ -15,6 +24,6 @@
 
 test_that("Printing corpusStats for a sub-corpus works", {
   skip_if_offline()
-  stats <- new("KorAPConnection") %>% corpusStats("pubDate in 2018")
+  stats <- new("KorAPConnection") %>% corpusStats("pubDate since 2020 & pubDate until 2021")
   expect_error(print(stats), NA)
 })