Skip to content

Commit

Permalink
If a Bible is specified in a workspace, activating that workspace set…
Browse files Browse the repository at this point in the history
…s the first Bible in that workspace as the active Bible

#1004
  • Loading branch information
teusbenschop committed Feb 12, 2025
1 parent b318066 commit 0910498
Show file tree
Hide file tree
Showing 5 changed files with 182 additions and 75 deletions.
1 change: 1 addition & 0 deletions help/changelog.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<h1>ChangeLog</h1>

<p>5.1.023: If a Bible is specified in a workspace, activating that workspace sets the first Bible in that workspace as the active Bible.</p>
<p>5.1.023: The verse editor preview sets the active Bible to the Bible loaded in that editor.</p>
<p>5.1.023: The editor switcher in the Bible editors sets the active Bible to the Bible loaded in the editor.</p>
<p>5.1.023: Embed the gumbo-parser code.</p>
Expand Down
212 changes: 139 additions & 73 deletions unittests/workspaces.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,96 +28,162 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#include <workspace/logic.h>


void test_workspaces_setup (Webserver_Request & request)
class workspaces : public testing::Test {
protected:
static void SetUpTestSuite() {
}
static void TearDownTestSuite() {
}
void SetUp() override {
refresh_sandbox (false);
}
void TearDown() override {
refresh_sandbox (true);
}
};


static void setup (Webserver_Request& request)
{
request.database_users ()->create ();
request.session_logic ()->set_username ("phpunit");
}


TEST (workspaces, basic)
TEST_F (workspaces, basic)
{
// Initial setup for the tests.
refresh_sandbox (false);
Webserver_Request webserver_request;
setup (webserver_request);

EXPECT_EQ ("100", workspace_process_units ("100"));
EXPECT_EQ ("1", workspace_process_units ("100 %"));
EXPECT_EQ ("1", workspace_process_units ("100 px"));

EXPECT_EQ ("Default", workspace_get_active_name (webserver_request));
webserver_request.database_config_user()->setActiveWorkspace ("unittest");
EXPECT_EQ ("unittest", workspace_get_active_name (webserver_request));

std::map <int, std::string> standard = {
std::pair (0, "editone/index"),
std::pair (5, "resource/index")
};
EXPECT_EQ (standard, workspace_get_default_urls (1));

{
Webserver_Request webserver_request;
test_workspaces_setup (webserver_request);

EXPECT_EQ ("100", workspace_process_units ("100"));
EXPECT_EQ ("1", workspace_process_units ("100 %"));
EXPECT_EQ ("1", workspace_process_units ("100 px"));

EXPECT_EQ ("Default", workspace_get_active_name (webserver_request));
webserver_request.database_config_user()->setActiveWorkspace ("unittest");
EXPECT_EQ ("unittest", workspace_get_active_name (webserver_request));

std::map <int, std::string> standard = {
std::pair (0, "editone/index"),
std::pair (5, "resource/index")
std::map <int, std::string> urls = {
std::pair (10, "url1"),
std::pair (2, "url2")
};
EXPECT_EQ (standard, workspace_get_default_urls (1));

{
std::map <int, std::string> urls = {
std::pair (10, "url1"),
std::pair (2, "url2")
};
workspace_set_urls (webserver_request, urls);
std::map <int, std::string> result = workspace_get_urls (webserver_request, false);
EXPECT_EQ (urls, result);
}
workspace_set_urls (webserver_request, urls);
std::map <int, std::string> result = workspace_get_urls (webserver_request, false);
EXPECT_EQ (urls, result);
}

{
std::map <int, std::string> widths = {
std::pair (0, "1"),
std::pair (1, "1"),
std::pair (2, "1"),
std::pair (3, "1")
};
std::map <int, std::string> result = workspace_get_widths (webserver_request);
EXPECT_EQ (widths, result);
}

{
std::vector <std::string> workspaces = workspace_get_names (webserver_request);
EXPECT_EQ (std::vector<std::string>{"unittest"}, workspaces);
}
{
std::map <int, std::string> widths = {
std::pair (0, "1"),
std::pair (1, "1"),
std::pair (2, "1"),
std::pair (3, "1")
};
std::map <int, std::string> result = workspace_get_widths (webserver_request);
EXPECT_EQ (widths, result);
}

refresh_sandbox (true);

{
Webserver_Request webserver_request;
test_workspaces_setup (webserver_request);
webserver_request.database_config_user()->setActiveWorkspace ("unittest");
workspace_set_urls (webserver_request, {std::pair (10, "url10")});
webserver_request.database_config_user()->setActiveWorkspace ("unittest2");
std::map <int, std::string> standard = { std::pair (0, "url0"), std::pair (5, "url5")};
workspace_set_urls (webserver_request, standard);
std::vector <std::string> workspaces = workspace_get_names (webserver_request);
EXPECT_EQ ((std::vector <std::string>{"unittest", "unittest2"}), workspaces);
workspace_delete (webserver_request, "unittest3");
workspaces = workspace_get_names (webserver_request);
EXPECT_EQ ((std::vector <std::string>{"unittest", "unittest2"}), workspaces);
workspace_delete (webserver_request, "unittest2");
workspaces = workspace_get_names (webserver_request);
EXPECT_EQ (std::vector <std::string>{"unittest"}, workspaces);
EXPECT_EQ (std::vector<std::string>{"unittest"}, workspaces);
}
}


TEST_F (workspaces, get_names_1)
{
Webserver_Request webserver_request;
setup (webserver_request);
webserver_request.database_config_user()->setActiveWorkspace ("unittest");
workspace_set_urls (webserver_request, {std::pair (10, "url10")});
webserver_request.database_config_user()->setActiveWorkspace ("unittest2");
std::map <int, std::string> standard = { std::pair (0, "url0"), std::pair (5, "url5")};
workspace_set_urls (webserver_request, standard);
std::vector <std::string> workspaces = workspace_get_names (webserver_request);
EXPECT_EQ ((std::vector <std::string>{"unittest", "unittest2"}), workspaces);
workspace_delete (webserver_request, "unittest3");
workspaces = workspace_get_names (webserver_request);
EXPECT_EQ ((std::vector <std::string>{"unittest", "unittest2"}), workspaces);
workspace_delete (webserver_request, "unittest2");
workspaces = workspace_get_names (webserver_request);
EXPECT_EQ (std::vector <std::string>{"unittest"}, workspaces);
}


TEST_F (workspaces, get_names_2)
{
Webserver_Request webserver_request;
setup (webserver_request);
webserver_request.database_config_user()->setActiveWorkspace ("unittest2");
workspace_set_urls (webserver_request, {std::pair (10, "url10")});
webserver_request.database_config_user()->setActiveWorkspace ("abc32");
workspace_set_urls (webserver_request, {std::pair (10, "url10"), std::pair (11, "url11")});
webserver_request.database_config_user()->setActiveWorkspace ("zzz");
workspace_set_urls (webserver_request, {std::pair (120, "url120"), std::pair (121, "url121")});
workspace_reorder (webserver_request, {"zzz", "yyy", "unittest2", "abc32"});
std::vector <std::string> workspaces = workspace_get_names (webserver_request);
EXPECT_EQ ((std::vector <std::string>{"zzz", "unittest2", "abc32"}), workspaces);
}

refresh_sandbox (true);

TEST_F (workspaces, get_bibles_from_urls)
{
{
Webserver_Request webserver_request;
test_workspaces_setup (webserver_request);
webserver_request.database_config_user()->setActiveWorkspace ("unittest2");
workspace_set_urls (webserver_request, {std::pair (10, "url10")});
webserver_request.database_config_user()->setActiveWorkspace ("abc32");
workspace_set_urls (webserver_request, {std::pair (10, "url10"), std::pair (11, "url11")});
webserver_request.database_config_user()->setActiveWorkspace ("zzz");
workspace_set_urls (webserver_request, {std::pair (120, "url120"), std::pair (121, "url121")});
workspace_reorder (webserver_request, {"zzz", "yyy", "unittest2", "abc32"});
std::vector <std::string> workspaces = workspace_get_names (webserver_request);
EXPECT_EQ ((std::vector <std::string>{"zzz", "unittest2", "abc32"}), workspaces);
const std::map <int,std::string> urls {
{1, "/a/b?bible=1&topbar=0"},
{2, "/a/b?topbar=0"},
{3, "/a/b?topbar=0&bible=2"},
{4, "/a/b?bible=3&topbar=0"},
{5, "/a/b?"},
{6, "/a/b"},
{7, "/a/b&bible=4"},
};
EXPECT_EQ ("1", get_first_bible_from_urls (urls));
}
{
const std::map <int,std::string> urls {
{1, "/a/b?topbar=0&bible=2"},
{2, "/a/b?bible=1&topbar=0"},
{3, "/a/b?topbar=0"},
{4, "/a/b?bible=3&topbar=0"},
{5, "/a/b?"},
{6, "/a/b"},
{7, "/a/b&bible=4"},
};
EXPECT_EQ ("2", get_first_bible_from_urls (urls));
}
{
const std::map <int,std::string> urls {
{1, "/a/b?topbar=0"},
{2, "/a/b?bible=3&topbar=0"},
{3, "/a/b?"},
{4, "/a/b"},
{5, "/a/b&bible=4"},
{6, "/a/b?topbar=0&bible=2"},
{7, "/a/b?bible=1&topbar=0"},
};
EXPECT_EQ ("3", get_first_bible_from_urls (urls));
}
{
const std::map <int,std::string> urls {
{1, "/a/b?topbar=0"},
{2, "/a/b?bible3=3&topbar=0"},
{3, "/a/b?"},
{4, "/a/b"},
{5, "/a/b&bible=4"},
{6, "/a/b?topbar=0&bible2=2"},
{7, "/a/b?bible1=1&topbar=0"},
};
EXPECT_FALSE (get_first_bible_from_urls (urls));
}
}

#endif

#endif
21 changes: 19 additions & 2 deletions workspace/index.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,12 @@ std::string workspace_index (Webserver_Request& webserver_request)
}


// If the workspace has specified a Bible project, then set this as the active Bible. Todo
{
const std::string workspace = webserver_request.database_config_user ()->getActiveWorkspace ();
}


// Create default set of workspaces if there are none.
bool create = workspaces.empty ();
if (!create) {
Expand Down Expand Up @@ -104,10 +110,11 @@ std::string workspace_index (Webserver_Request& webserver_request)

std::map <int, std::string> urls = workspace_get_urls (webserver_request, true);
std::map <int, std::string> widths = workspace_get_widths (webserver_request);
// The Bible editor number, starting from 1, going up.
// The Bible editor number, starting from 1, increasing.
std::map <int, int> editor_numbers = workspace_add_bible_editor_number (urls);
for (int key = 0; key < 15; key++) {
const std::string url = urls [key];
const std::string url = urls [key]; // Todo
if (!url.empty()) std::cout << url << std::endl; // Todo
const std::string width = widths [key];
const int editor_number = editor_numbers [key];
const int row = static_cast<int> (round (key / 5)) + 1;
Expand Down Expand Up @@ -142,6 +149,16 @@ std::string workspace_index (Webserver_Request& webserver_request)
view.set_variable ("workspacewidth", workspacewidth);


// When the URLs loaded specify a Bible, then set this as the active Bible.
// Extract the first Bible, if any, to do that.
// See https://github.com/bibledit/cloud/issues/1004 for more info.
{
std::optional<std::string> bible = get_first_bible_from_urls (urls);
if (bible)
webserver_request.database_config_user()->setBible(bible.value());
}


// The rendered template disables framekillers through the "sandbox" attribute on the iframe elements.
page += view.render ("workspace", "index");
page += assets_page::footer ();
Expand Down
22 changes: 22 additions & 0 deletions workspace/logic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@
#include <database/logs.h>
#include <database/cache.h>
#include <read/index.h>
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Weffc++"
#include <parsewebdata/ParseWebData.h>
#pragma GCC diagnostic pop


std::vector <std::string> workspace_get_default_names ()
Expand Down Expand Up @@ -577,3 +581,21 @@ std::map <int, int> workspace_add_bible_editor_number (std::map <int, std::strin
return editor_numbers;
}


std::optional<std::string> get_first_bible_from_urls (const std::map <int,std::string>& urls) // Todo
{
for (const auto& [key, url] : urls) {
const std::vector <std::string> bits = filter::strings::explode (url, '?');
if (bits.size() != 2)
continue;
if (!bits.at(1).empty ()) {
ParseWebData::WebDataMap web_data_map;
ParseWebData::parse_get_data (bits.at(1), web_data_map);
for (auto iter = web_data_map.cbegin(); iter != web_data_map.cend(); ++iter) {
if ((*iter).first == "bible")
return filter_url_urldecode ((*iter).second.value);
}
}
}
return std::nullopt;
}
1 change: 1 addition & 0 deletions workspace/logic.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,4 @@ void workspace_cache_for_cloud (Webserver_Request& webserver_request, bool urls,
std::string workspace_get_default_name ();
void workspace_send (Webserver_Request& webserver_request, std::string workspace, std::string user);
std::map <int, int> workspace_add_bible_editor_number (std::map <int, std::string> & urls);
std::optional<std::string> get_first_bible_from_urls (const std::map <int,std::string>& urls);

0 comments on commit 0910498

Please sign in to comment.